Android:将参数传递给Service from Activity

Ily*_*okh 19 service binding android parameter-passing android-activity

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

活动类:

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) {
        }
    };
Run Code Online (Sandbox Code Playgroud)

这是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;
}
Run Code Online (Sandbox Code Playgroud)

所以我有"null" LogCat- 尽管我intent.putExtra之前做过,但bundle仍然是nullbindService

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

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

Dee*_*rma 32

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

Intent serviceIntent = new Intent(this,ListenLocationService.class); 
   serviceIntent.putExtra("From", "Main");
   startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)

并在服务类的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();
}

}
Run Code Online (Sandbox Code Playgroud)

请享用 :)

  • 这是根据请求将参数传递给onBind方法的方法.@yorkw有一个很好的通用解决方案来调用服务中的Activity. (2认同)

yor*_*rkw 9

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

public interface ILocationService {
  public void StartListenLocation(Location location);
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

  ... ...
}
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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

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

希望这可以帮助.

  • @DuneCat,我同意这个说法不太准确,实际上它是谷歌建议实施绑定服务的推荐方式之一,见[这里](http://developer.android.com/guide/components/bound- services.html #Binder)了解更多详情. (2认同)