我通过这种方式绑定到服务:
活动类:
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
的活动
发布于 2012-03-31 18:28:21
1创建一个接口,该接口声明要从活动中调用的所有方法签名:
public interface ILocationService {
public void StartListenLocation(Location location);
}
2使你的绑定器实现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);
所有通信(即对服务的方法调用)都应该通过绑定类initialize并在ServiceConnection.onServiceConnected()中返回来处理和执行,而不是实际的服务类(binder.getService()是不必要的)。这就是bind服务通信在API中工作的方式。
请注意,bindService()是一个异步调用。在调用bindService()之后和系统涉及到ServiceConnection.onServiceConnected()回调之前,会有一个延迟。因此,执行服务方法的最佳位置是在ServiceConnection.onServiceConnected()方法中初始化mService之后立即执行。
希望这能有所帮助。
发布于 2013-05-28 20:16:48
您可以通过以下简单的方式传递参数:-
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();
}
}
享受:)
发布于 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,如上一个答案所示,根据您的应用程序进行选择。
https://stackoverflow.com/questions/9954878
复制相似问题