0

我正在使用倒数计时器类,并且在该类的 onfinish() 状态下,我正在创建一个通知。这是 Onfinish 方法的代码:

public void onFinish() {
            notificate();
            Log.i("Aler Resume", "Finished");
            remtime.setText("0 : 00 : 00");
            textCondition.setText(getString(R.string.BuddyAlertExpired));
              }

在我写的 notify() 方法中:

private void notificate() {
        NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  

        Notification note = new Notification(R.drawable.ic_launcher, "New E-mail", System.currentTimeMillis());  

         PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, AlertResume.class), 0); 
         note.setLatestEventInfo(this, "Buddy Alert", getString(R.string.BuddyAlertExpired), intent); 
         note.defaults= Notification.DEFAULT_VIBRATE;
         note.defaults=Notification.DEFAULT_SOUND;
         notifManager.notify(NOTIF_ID, note); 


    }

当我保持我的应用程序打开时它工作正常。但是当我从应用程序返回时,正在调用 onfinish 方法,我可以正确查看日志,但 notofication 方法不起作用并且不显示任何通知。有什么错误吗?

4

1 回答 1

0

当您离开应用程序时,Android 可能会认为您运行倒计时计时器的进程是垃圾邮件或额外邮件,而不是有意的。并完成它......(就像它认为你忘记关闭让我关闭)

所以最好将 AlarmManager 用于上述内容,例如设置 PendingIntent - 通知,在您启动倒数计时器的时间还使用警报管理器设置带有未决意图的通知。

        nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.icon, "Test", System.currentTimeMillis());

        Intent intentTL = new Intent(context, MainActivity.class);
        notification.setLatestEventInfo(context, "Test", "Do something!",
                PendingIntent.getActivity(context, 0, intentTL,
                PendingIntent.FLAG_CANCEL_CURRENT));
        notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
        nm.notify(1, notification);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pendingIntent);
于 2012-04-10T06:20:54.323 回答