报警和通知

时间:2014-03-23 10:12:12

标签: android

我找到了这个警报通知的例子,我想请你改变两件事 这是MainActivity:

public void setRepeatingAlarm() {
      Intent intent = new Intent(this, MyAlarmService.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_CANCEL_CURRENT);
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
        (129600000), pendingIntent);
     }

这是MyAlarmService:

public class MyAlarmService extends BroadcastReceiver {

 NotificationManager nm;

 @Override
 public void onReceive(Context context, Intent intent) {
  nm = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
  CharSequence from = "Locali Torino";
  CharSequence message = "Visita le serate!";
  PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    new Intent(), 0);

  Notification notif = new Notification(R.drawable.disco,
    "Visita le serate!", System.currentTimeMillis());
  notif.setLatestEventInfo(context, from, message, contentIntent);
  nm.notify(1, notif);
 }
}

通过这种方式,当我启动我的应用程序时,现在我看到了通知,但我希望看到它只在“x”毫秒后才这样做。
然后我想知道如何启动MainActivity点击通知 谢谢。

1 个答案:

答案 0 :(得分:0)

稍稍调用setRepeatingAlarm()。从服务或活动使用,例如处理程序并以小延迟发布可运行的程序。如果您正在进行活动,请不要忘记在生命周期方法中删除帖子操作,例如的onStop()。如果您想在用户点击通知时进行某些操作,请在MyAlarmService中修改contentIntent

E.g。

    Intent action = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    action, 0);