我通过这种方式绑定到服务:
活动类:
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“--尽管我在bindService
之前做了intent.putExtra
,但捆绑包是null
一般来说,Service工作得很好。但是,我只需要从应用程序的主活动调用StartListenLocation();
(我决定通过发送一个标志来实现这一点)。
如何将数据发送到服务?或者有另一种方法来检查启动了onBind
的活动
https://stackoverflow.com/questions/9954878
复制相似问题