Android:参数从活动传递把服务
问题说明
我通过这种方式绑定到服务:
I'm binding to Service by this way:
活动类:
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
So I have "null" in LogCat
- bundle is null in spite of I made intent.putExtra
before bindService
总的来说,服务工作正常.但是我只需要从应用程序的主要活动中调用 StartListenLocation();
(我决定通过发送一个标志来做到这一点).
In general Service works fine. However I need to call StartListenLocation();
only from main activity of application (I decide to do this by sending a flag).
我如何向服务发送数据?或者也许有另一种方法来检查启动了哪个活动 onBind
?
How can I send a data to service?Or maybe there's another way to check what activity launched onBind
?
正确答案
1 创建一个接口,声明您要从 Activity 调用的所有方法签名:
1 Create a interface that declare all method signature that you want to call from a Activity:
public interface ILocationService {
public void StartListenLocation(Location location);
}
2 让你的 binder 实现 ILocaionService 并定义实际的方法体:
2 Make your binder implements ILocaionService and define the actual method body:
public class MyBinder extends Binder implements ILocationService {
... ...
public void StartListenLocation(Location location) {
// implement your method properly
}
... ...
}
3 在绑定到服务的活动中,通过接口引用您的绑定器:
3 In activity that bind to the service, reference your binder by the interface:
... ...
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);
所有通信(即对您的服务的方法调用)都应该通过在 ServiceConnection.onServiceConnected() 中初始化和返回的 binder 类来处理和执行,而不是实际的服务类(binder.getService() 是不必要的).这就是设计用于 API 中的绑定服务通信的方式.
All communications (i.e. method call to your Service) should be handled and performed via the binder class initialize and return in ServiceConnection.onServiceConnected(), not the actual service class (binder.getService() is unnecessary). This is how bind service communication designed to work in the API.
注意 bindService() 是一个异步调用.在您调用 bindService() 之后和在 ServiceConnection.onServiceConnected() 回调被系统参与之前会有一个延迟.所以最好在 ServiceConnection.onServiceConnected() 方法中初始化 mService 之后立即执行 service 方法.
Note that bindService() is an asynchronous call. There will be a lag after you call bindService() and before ServiceConnection.onServiceConnected() callback get involved by system. So the best place to perform service method is immediately after mService get initialized in ServiceConnection.onServiceConnected() method.
希望这会有所帮助.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjecc
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20