Android:将参数传递给Service from Activity

时间:2012-03-31 09:54:16

标签: android binding service android-activity parameter-passing

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

活动类:

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方法:

@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” - 尽管我在intent.putExtra

之前做了bindService,但bundle仍为null

一般情况下服务工作正常。但是我只需要从应用程序的主要活动中调用StartListenLocation();(我决定通过发送标志来执行此操作)。

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

3 个答案:

答案 0 :(得分:31)

您可以用这种简单的方式传递参数: -

Intent serviceIntent = new Intent(this,ListenLocationService.class); 
   serviceIntent.putExtra("From", "Main");
   startService(serviceIntent);

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

    @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();
}

}

享受:)

答案 1 :(得分:8)

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

public interface ILocationService {
  public void StartListenLocation(Location location);
}

2让您的binder实现ILocaionService并定义实际的方法体:

public class MyBinder extends Binder implements ILocationService {
  ... ...

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

  ... ...
}

3在绑定到服务的活动中,通过界面引用您的活页夹:

... ...

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);

应该通过binder类初始化并在ServiceConnection.onServiceConnected()中返回所有通信(即对您的服务的方法调用),而不是实际的服务类(binder.getService()是不必要的)。这就是设计用于API的绑定服务通信的方式。

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

希望这有帮助。

答案 2 :(得分:0)

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

将数据发送到服务的一种方法是使用包含列pid(进程ID),数据,状态可以是READ,READY,BINDING等的数据库,当服务加载时它会读取由呼叫活动填写的表格,完成后可以删除,另一种方式是AIDL,如上一个答案所示,根据您的申请选择。