java - 服务启动时显示的Android通知在创建后立即取消

标签 java android android-service android-notifications

我正在测试基于 Remote Messenger Service Sample 制作一个小型应用程序

我有一个带有按钮的 Activity 以启动/停止服务。 启动服务后,我按照示例中的描述绑定(bind)服务和 Activity ,一切都按预期进行。 该服务有一个通知,就像示例一样:通知在 MessengerService.onCreate() 中显示,在 MessengerService.onDestroy() 中取消。

现在,我想在 Activity 出现时自动启动服务。我的 Activity 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 启动应用程序进行测试时, Activity 出现了,我可以短暂地看到通知,然后,通知在大约一秒钟后消失。 该服务仍然存在,因为我可以发送消息以强制通知返回。

经过一些调查,我在事件日志中看到:

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 启动应用程序时,似乎发生了以下情况:

  • 包改了
  • Activity 开始(并出现)
  • 服务启动
  • 通知可见
  • 系统收到PACKAGE_REMOVED/PACKAGE_ADDED并移除该包的所有通知
  • 通知被驳回

我的问题:如何保留通知而不被系统取消?

一些补充说明:

  • 有时(大约 10% 的时间),通知不会被取消,一切都按预期进行。但大多数时候,这种行为是错误的。
  • 当我在安装后从启动器启动应用程序时,而不是从 Android Studio 启动应用程序时,一切都很好。
  • 在这些日志中,服务在另一个进程中启动( list 中的 android:process=":notification"),但在同一进程中启动不会改变行为。
  • 这些测试是在装有 Android 4.3 的 Galaxy Nexus 设备上完成的

编辑:

在这个例子中,我了解到在“真实”的应用程序中,用户可能不会在升级应用程序之后和系统正确关闭之前的包之前打开 Activity 。我将 startService 从 Activity 中移出,放在 PACKAGE_REPLACED 接收器中(并处理所有内容以启动服务,只有当它在新版本之前运行时),但是这个 PACKAGE_REPLACED 在我的通知被取消后大约 20 秒收到了 Intent 。所以我还有一些时间让我的服务做一些事情(例如播放音乐),并在 20 秒后弹出合适的通知。

最佳答案

您看到了竞争条件,因为您立即绑定(bind)服务并显示来自它的通知。这可能不会引起太多注意,因为通常应用程序不会在其主要 Activity 的 onCreate Activity 中立即显示通知。

尝试将您的 doBindService 替换为

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        doBindService();
    }
}, 1000);

或者,如果您只是在测试,请不要担心。

关于java - 服务启动时显示的Android通知在创建后立即取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21931762/

相关文章:

Android 后台服务控制 UI Activity

java - spring boot 向环境添加另一个属性文件

java - 在 android 中实现推荐技术/应用程序邀请的最佳方式?

在 `runnableThread` 类中运行新的 `Service` 时出现 java.lang.ClassCastException

android - 从单例绑定(bind)服务

android - 为什么 Service 在 Activity 被销毁时会 self 销毁?

java - Jolokia - Origin null 不允许调用此代理

U 盘上的 Java 软件

java - java.util.function.Consumer 的 C++ 等价物是什么?

java - Eclipse ADB 未启动