【问题标题】:android Local notification while app is stopped应用程序停止时的android本地通知
【发布时间】:2015-01-04 19:34:03
【问题描述】:

在我的应用程序关闭后的一段时间,我已经从 SO 关注 some tutorials,通过向我的自定义 BroadcastReceiver 实现广播,使用 AlertManager 生成本地通知。效果很好,我可以在设备的通知区域看到通知。

但是,它仅在应用的主要活动暂停时才有效。如果应用程序停止(通过设置->应用程序),没有通知,广播接收器永远不会被调用。我的印象是 AlertManager 在某些操作系统服务中注册我的通知请求 - 与我的应用程序无关,这就是重点,有某种通知,用户可以通过它重新启动我的应用程序。我正在测试 Android 4.2.1 BTW。有没有可能我只是做错了什么,实际上有一种方法可以让 AlertManager 成功广播某些内容?

这是我的 AlertManager 代码,从我的主要活动的 onPause 调用(设置为 10 秒,仅用于测试)。 'ctx' 是主要活动

     Calendar cal = Calendar.getInstance();
     cal.add(Calendar.SECOND, 10);
     Intent intent = new Intent(ctx, MyAlarmReceiver.class);
     intent.putExtra("alarm_message", "hey, wake up this app!");
     // note: 192837 is just some test ID:
     PendingIntent sender = PendingIntent.getBroadcast(ctx, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     // Get the AlarmManager service
     AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);        

这里是 MyAlarmReceiver.onReceive(context, intent):

    try {
         Bundle bundle = intent.getExtras();
         String message = bundle.getString("alarm_message");
         NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Title!!!")
                    .setContentText(message);
            Intent resultIntent = new Intent(context, MainActivity.class);
            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(context, 192838, resultIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(123423, mBuilder.build());
        } catch (Exception e) {
         Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
         e.printStackTrace();
        }       

【问题讨论】:

    标签: android android-intent notifications broadcastreceiver


    【解决方案1】:

    总而言之,强制关闭您的应用意味着用户明确表示他 不想再运行你的应用了

    从 3.1 开始,当安装应用程序时,它们处于“停止”状态,因此在用户明确启动它们之前它们将无法运行。按下强制停止将使它们返回此状态,因此如果用户强制停止您的应用程序,则应用程序的所有组件(BroadcastReceivers、Services、AlarmManager...)将不再工作,直到用户再次手动运行您的应用程序。 这在 3.1 发行说明 here 中有记录。

    虽然AlarmManager 的文档指出

    注意:警报管理器适用于您希望拥有的情况 您的应用程序代码在特定时间运行,即使您的应用程序 当前未运行。对于正常的计时操作(滴答声、 超时等)使用 Handler 更容易也更高效。

    在您的应用程序被强制关闭后它也不会工作,这不是一个错误,它是一个功能。

    Android 框架开发者https://groups.google.com/forum/?fromgroups=#!topic/android-developers/anUoem0qrxU确认此行为

    【讨论】:

    • 那么也许有使用其他机制的解决方案?
    • 我觉得你可以注册一个BroadcastReceiver for Boot completed重新打开,否则我不这么认为。
    • 更新:您的原始答案与 AlarmManager 文档冲突:“警报管理器适用于您希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行”,这意味着当我的应用程序未运行时,上述内容应该可以工作,但它不会......这意味着问题是使用 BroadcastReceiver 而不是其他东西。我会检查并更新。
    • 我刚刚做了另一个测试,尝试运行服务 (pendingIntent.getService) 而不是发送广播 (pendingIntent.getBroadcast) - 结果相同。所以它与BroadcastReceivers无关,根据logcat,AlarmManager服务不会调用意图,虽然它是一个运行在不同进程中的操作系统远程服务,如果我理解正确的话。
    • 谢谢,我还下载了一些领先的安卓游戏,并验证了同样的行为......对他们有好处的对我有好处:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多