gpt4 book ai didi

android - Android 服务的良好做法

转载 作者:行者123 更新时间:2023-11-29 02:09:26 25 4
gpt4 key购买 nike

我目前在我的应用中使用 2 项服务:

1:LocationService,主要是尝试定位用户,旨在仅当应用程序处于前台时才保持 Activity 状态。

2:XmppService,它初始化与 xmpp 服务器的连接、接收消息、发送消息、注销……并旨在保持 Activity 状态直到用户注销。

我已经阅读了很多文档,但我就是说不清楚。

当我尝试存储用于调用我的服务函数(使用 AIDL 接口(interface))的 LocationServiceBinder 的引用时,我遇到了泄漏 ). Xmpp 也一样。当我解除绑定(bind)时,我有时会收到 ANR(这似乎与我的绑定(bind)/解除绑定(bind)执行异常、onResume、onRestart ... 的事实有关)。

所有系统都在工作,但我确定这不是正确的方法,我很乐意跟随有经验的人回到原力的右边! :)

干杯

更新

我的位置服务在应用程序启动时绑定(bind),以尽可能快地获取用户的位置:

if(callConnectService == null) {
callConnectService = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder binder) {
locationServiceBinder = LocationServiceBinder.Stub.asInterface(binder);
try {
global.setLocationBinder(locationServiceBinder);
global.getLocationBinder().startLocationListener();
} catch (Exception e){
Log.e(TAG, "Service binder ERROR");
}
}

public void onServiceDisconnected(ComponentName name) {
locationServiceBinder = null;
}
};
}

/* Launch Service */
aimConServ = new Intent(this, LocationService.class);
boolean bound = bindService(aimConServ,callConnectService,BIND_AUTO_CREATE);

我的 Xmpp 服务在用户登录时启动:

callConnectService = new ServiceConnection() {

            public void onServiceConnected(ComponentName name, IBinder binder) {
try {
Log.d(TAG, "[XMPP_INIT] Complete.");
global.setServiceBinder(ConnectionServiceBinder.Stub.asInterface(binder));
//Connect to XMPP chat
global.getServiceBinder().connect();
} catch (Exception e){
Log.e(TAG, "Service binder ERROR ");
e.printStackTrace();
}
}

public void onServiceDisconnected(ComponentName name) {
Log.e(TAG, "Service binder disconnection ");
}
};

/* Launch Service */
Intent aimConServ = new Intent(MMWelcomeProfile.this, XmppService.class);
bound = bindService(aimConServ,callConnectService,Context.BIND_AUTO_CREATE);

并在每个 Activity 上解除绑定(bind):

if (callConnectService != null){
unbindService(callConnectService);
callConnectService = null;
}

最佳答案

在谷歌的官方开发指南中并没有详细说明,Context.bindService()实际上是一个异步调用。这就是为什么将 ServiceConnection.onServiceConnected() 用作回调方法的原因,意味着不会立即发生。

public class MyActivity extends Activity {
private MyServiceBinder myServiceBinder;

protected ServiceConnection myServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
myServiceBinder = (MyServiceBinderImpl) service;
}

... ...
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// bindService() is an asynchronous call. myServiceBinder is resoloved in onServiceConnected()
bindService(new Intent(this, MyService.class),myServiceConnection, Context.BIND_AUTO_CREATE);
// You will get a null point reference here, if you try to use MyServiceBinder immediately.
MyServiceBinder.doSomething(); // <-- not yet resolved so Null point reference here
}
}

解决方法是在 myServiceConnection.onServiceConnected() 中调用 MyServiceBinder.doSomething(),或通过某些用户交互(例如单击按钮)执行 MyServiceBinder.doSomething(),因为在调用 bindService() 之后和系统获得myServiceBinder 的引用很快。只要您不立即使用它,就应该没问题。

查看这个 SO 问题 CommonsWare's answer了解更多详情。

关于android - Android 服务的良好做法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8341782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com