首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:从Activity传递参数给Service

Android:从Activity传递参数给Service
EN

Stack Overflow用户
提问于 2012-03-31 17:54:16
回答 3查看 42.7K关注 0票数 27

我通过这种方式绑定到服务:

活动类:

代码语言:javascript
复制
ListenLocationService mService;
@Override
public void onCreate(Bundle savedInstanceState) {
        ...
        Intent intent = new Intent(this, ListenLocationService.class);
        intent.putExtra("From", "Main");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        ...
}

private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,
                IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();         
        }

        public void onServiceDisconnected(ComponentName arg0) {
        }
    };

这是我的服务onBind方法

代码语言:javascript
复制
@Override
public IBinder onBind(Intent intent) {
    Bundle extras = intent.getExtras(); 
    if(extras == null)
        Log.d("Service","null");
    else
    {
        Log.d("Service","not null");
        String from = (String) extras.get("From");
        if(from.equalsIgnoreCase("Main"))
            StartListenLocation();
    }
    return mBinder;
}

所以我在LogCat中有" null“--尽管我在bindService之前做了intent.putExtra,但捆绑包是null

一般来说,Service工作得很好。但是,我只需要从应用程序的主活动调用StartListenLocation(); (我决定通过发送一个标志来实现这一点)。

如何将数据发送到服务?或者有另一种方法来检查启动了onBind的活动

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-31 18:28:21

1创建一个接口,该接口声明要从活动中调用的所有方法签名:

代码语言:javascript
复制
public interface ILocationService {
  public void StartListenLocation(Location location);
}

2使你的绑定器实现ILocaionService并定义实际的方法体:

代码语言:javascript
复制
public class MyBinder extends Binder implements ILocationService {
  ... ...

  public void StartListenLocation(Location location) {
    // implement your method properly
  }

  ... ...
}

3在绑定到服务的活动中,通过接口引用您的绑定器:

代码语言:javascript
复制
... ...

ILocationService mService; // communication is handled via Binder not the actual service class.

private ServiceConnection mConnection = new ServiceConnection() {

  public void onServiceConnected(ComponentName className, IBinder service) {
    mService = (ILocationService) service;     
  }

  ... ...
};

... ...

// At some point if you need call service method with parameter:
Location location = new Location();
mService.StartListenLocation(location);

所有通信(即对服务的方法调用)都应该通过绑定类initialize并在ServiceConnection.onServiceConnected()中返回来处理和执行,而不是实际的服务类(binder.getService()是不必要的)。这就是bind服务通信在API中工作的方式。

请注意,bindService()是一个异步调用。在调用bindService()之后和系统涉及到ServiceConnection.onServiceConnected()回调之前,会有一个延迟。因此,执行服务方法的最佳位置是在ServiceConnection.onServiceConnected()方法中初始化mService之后立即执行。

希望这能有所帮助。

票数 13
EN

Stack Overflow用户

发布于 2013-05-28 20:16:48

您可以通过以下简单的方式传递参数:-

代码语言:javascript
复制
Intent serviceIntent = new Intent(this,ListenLocationService.class); 
   serviceIntent.putExtra("From", "Main");
   startService(serviceIntent);

并获取服务类的onStart方法中的参数

代码语言:javascript
复制
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Bundle extras = intent.getExtras(); 
    if (extras == null) {
        Log.d("Service","null");
    else {
        Log.d("Service","not null");
        String from = (String) extras.get("From");
        if(from.equalsIgnoreCase("Main"))
        StartListenLocation();
    }
}

享受:)

票数 46
EN

Stack Overflow用户

发布于 2012-03-31 20:55:42

根据此会话http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html

向服务发送数据的一种方法是使用包含列pid(进程id)、数据、状态的数据库,其中状态可以是READ、READY、BINDING等等,当服务加载时,它读取由调用活动填充的表,完成后可以删除,另一种方式是AIDL,如上一个答案所示,根据您的应用程序进行选择。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9954878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档