我的测试,以一个小的应用程序后取消的基础上,Remote Messenger Service SampleAndroid的通知刚刚创建
我有一个按钮的活动,以启动/停止服务。 启动服务后,我将服务和活动绑定在一起,如示例中所述,并且所有内容都按预期工作。 该服务有一个通知,就像样本一样:通知显示在MessengerService.onCreate()
中,并在MessengerService.onDestroy()
中取消。
现在,我想在活动出现时自动启动服务。我的活动onStart()
情况如下(我删除了错误检查代码,以使代码更易读):从样品采取
@Override
public void onStart()
{
super.onStart();
Log.d(TAG, "start service at activity startup");
// Start the service automatically
Intent intent = new Intent(this, MessengerService.class);
startService(intent);
// Bind to the service
doBindService();
}
随着doBingService():
void doBindService()
{
Log.d(TAG, "doBindService call");
// Establish a connection with the service. We use an explicit
// class name because there is no reason to be able to let other
// applications replace our component.
bindService(new Intent(this, MessengerService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
现在,当我启动应用程序从Android Studio进行测试,活动就会显示出来,我可以简单地看到通知,然后,通知在大约一秒钟后消失。 该服务仍然存在,因为我可以发送消息来强制通知返回。
02-20 22:35:03.342 383 396 I notification_cancel_all: [com.example.messengerservice,-1,0,0]
02-20 22:35:03.545 383 396 I notification_cancel_all: [com.example.messengerservice,-1,0,0]
以及相应的系统日志:
一些调查后,我在事件日志中看到
02-20 22:35:01.154 22884 22884 D example.Activity: start service at activity startup
02-20 22:35:01.170 22884 22884 D example.Activity: doBindService call
02-20 22:35:01.170 383 3814 I ActivityManager: Start proc com.example.messengerservice:notification for service com.example.messengerservice/.MessengerService: pid=22938 uid=10105 gids={50105, 1028}
02-20 22:35:01.232 22938 22938 I example.MessengerService: Service created
02-20 22:35:01.232 22938 22938 D example.MessengerService: Service bound
02-20 22:35:01.232 22938 22938 D example.MessengerService: Service start command
02-20 22:35:01.240 22938 22938 V example.MessengerService: Show notification
[...]
02-20 22:35:03.232 383 3801 I ActivityManager: Start proc com.google.android.apps.plus for broadcast com.google.android.apps.plus/.service.PackagesMediaMonitor: pid=23005 uid=10044 gids={50044, 3003, 3002, 1015, 1006, 1028}
02-20 22:35:03.271 22955 22957 D dalvikvm: GC_CONCURRENT freed 438K, 5% free 9179K/9656K, paused 2ms+4ms, total 28ms
02-20 22:35:03.271 22955 22955 D dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 21ms
02-20 22:35:03.357 383 396 D BackupManagerService: Received broadcast Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:com.example.messengerservice flg=0x8000010 (has extras) }
02-20 22:35:03.537 22837 22859 W GAV2 : Thread[GAThread,5,main]: Service unavailable (code=1), will retry.
02-20 22:35:03.537 383 394 W ActivityManager: Unable to start service Intent { act=com.google.android.gms.analytics.service.START (has extras) } U=0: not found
02-20 22:35:03.545 383 396 D BackupManagerService: Received broadcast Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.example.messengerservice flg=0x8000010 (has extras) }
02-20 22:35:03.545 383 396 V BackupManagerService: removePackageParticipantsLocked: uid=10105 #1
02-20 22:35:03.545 383 396 V BackupManagerService: addPackageParticipantsLocked: #1
似乎开始从Android Studio中的应用程序时,我有以下发生的事情:
- 包被更改
- 活动开始(和显示出来)
- 服务已启动
- 通知是可见
- 系统接收PACKAGE_REMOVED/PACKAGE_ADDED并移除此包
- 通知是驳回的所有通知
我的问题:如何保持通知而不被系统取消?
一些其他注意事项:
- 有时(时代约10%),该通知没有被取消,一切都按预期工作。但大多数时候,这种行为已被打破。
- 当我从启动程序启动应用程序一旦安装,而不是从Android Studio,一切都很好。
- 在这些日志中,服务在另一个进程(清单中的android:process =“:notification”)中启动,但在同一进程中启动不会改变行为。
- 这些测试是在Galaxy Nexus设备上完成的,安卓4。3
编辑:
在这个例子中,我了解到,在“真正的”应用程序,用户将propably不只是升级应用程序后,打开活动,并在系统之前正确关闭以前包。我将startService移出活动,在PACKAGE_REPLACED
接收器中(并且只有在新版本之前运行时才处理所有内容以启动服务),但是在我的通知被取消后大约20秒后会收到这个PACKAGE_REPLACED
意图。所以我还有一段时间,我的服务正在做某些事情(例如播放音乐),并在20秒后弹出适当的通知。
通知从服务的onCreate()方法开始,而不是onBind()所以我也把startService()放入你的run()中。这比较好,但在Android启动后超过1秒的时间内,Android仍然会杀死我的通知。当然,我可以延长这个延迟,但它看起来并不是一直在工作的“干净”解决方案。 – biskitt
我编辑了这个问题,试图解释为什么在这个例子中我可以忍受它,但是在“真实世界”应用程序(例如音乐播放器)中,我对这种行为并不满意。我想不出有什么办法可以随时通知我的服务。 – biskitt
为什么媒体播放器会在创建活动时立即显示通知?通常,如果用户正在查看您的应用程序,他们也不需要通知它。通常在您的应用处于后台时显示。 –