- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前在我的应用中使用 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/
嗨,我正在考虑开发一种文件传输程序,想知道我是否想要尽可能好的加密,我应该使用什么? 我会用 C# 开发它,所以我可以访问 .net 库 :P在我的 usb 上有一个证书来访问服务器是没有问题的,如果
我创建的这个计算两个数组的交集是线性的方法的复杂度(在良好、平均、最差的情况下)? O(n) public void getInt(int[] a,int[] b){ int i=0; int
我已经能够使用 RTCPeerConnection.getStats() API 获得 WebRTC 音频调用的各种统计信息(抖动、RTT、丢包等)。 我需要将整体通话质量评为优秀、良好、一般或差。
基本问题: 如果我正在讲述/修改数据,我应该通过索引硬编码索引访问文件的元素,即 targetFile.getElement(5);通过硬编码标识符(内部翻译成索引),即 target.getElem
在 Linux 上,我想知道要调用什么“C”API 来获取每个 CPU 的统计信息。 我知道并且可以从我的应用程序中读取 /proc/loadavg,但这是系统范围的负载平均值,而不是每个 CPU 的
在客户端浏览器中使用 fetch api,GET 或 POST 没有问题,但 fetch 和 DELETE 有问题。它似乎将 DELETE 请求方法更改为 OPTIONS。 大多数研究表明是一个cor
我是一名优秀的程序员,十分优秀!