【问题标题】:How to get local notification in Android while Application stop?应用程序停止时如何在 Android 中获取本地通知?
【发布时间】:2011-07-28 13:00:16
【问题描述】:

我正在开发一个使用本地通知的 Android 应用程序 通过使用警报管理器和广播接收器。我收到通知,默认情况下没有任何错误。如果我使用设置-> 应用程序-> ManageApplication-> MyApplication-> ForceStop 强制停止我的应用程序,我没有收到通知。我的代码是

安排活动

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, 2);
        Intent intent = new Intent(this, AlarmReceiver.class);
        intent.putExtra("alarm_message", "O'Doyle Rules!");
        PendingIntent sender = PendingIntent.getBroadcast(this, 192837,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

我的广播接收器

try 
        {
             Bundle bundle = intent.getExtras();
             String message = bundle.getString("alarm_message");
             Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
             Intent i=new Intent(context,AlertActivity.class);
             i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
             context.startActivity(i);     
        } 
        catch (Exception e) 
        {     
            e.printStackTrace();
        }

请给我一些建议来解决我的问题。提前致谢

【问题讨论】:

  • 你能显示你的 logcat 错误,以便我试着找出你出错的地方
  • 我没有收到任何日志猫错误

标签: android notifications


【解决方案1】:

你能再解释一下吗...我猜如果不使用任何后台服务或应用程序小部件之类的东西,你怎么能期望你的应用程序即使在强制关闭后也能继续运行???

【讨论】:

  • 1.设置闹钟并关闭闹钟(使用 taskiller)后,闹钟永远不会响起。为什么?我认为这是意图和广播接收器背后的想法。另外,android可以因为需要资源而杀死警报吗?这可能吗?
【解决方案2】:

这不起作用,您必须执行以下操作:

Calendar cal = Calendar.getInstance(); 
cal.setTimeInMillis(SystemClock.elapsedRealtime());
cal.add(Calendar.MINUTE, 2);

...等等...

【讨论】:

    【解决方案3】:

    API forcestoppackage 将取消与包关联的警报 所以以后什么都不会发生。

    forcestopPackage() 将发送一个带有动作“package_restart”的广播,AlarmManagerService 监听这个广播并移除所有被强制停止的应用设置的警报。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多