我有3项活动
一个。 MainActivity - 只是一个带有文本和按钮的活动
B中。 SettingsActivity - 如果单击(A)上的按钮,将显示此项,这将初始化时间选择器,该时间选择器将在发出警报时发送通知
℃。 DisplayNotification - 用于在状态栏中显示通知的活动,单击时,它应显示A
这就是我想要发生的事情:
这就是发生的事情(我有两个不同结果的情景):
第一种情况:
第二种情况:(越野车)
我是否遗漏了通知或意图的任何标志?
以下是活动B的代码片段,它触发警报
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, DisplayNotification.class);
i.putExtra("NotifID", 1);
PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), 0, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, displayIntent);
以下是活动C的代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int notifID = getIntent().getExtras().getInt("NotifID");
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("NotifID", notifID);
PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "See activity!", System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
CharSequence from = "Notification from me";
CharSequence message = "See activity!";
notif.setLatestEventInfo(this, from, message, detailsIntent);
nm.notify(notifID, notif);
finish();
}
我希望有人能帮助我:)。
谢谢!
编辑:
谢谢Chirag_CID!我应该使用广播接收器而不是另一个活动。因此,DisplayNotification不再扩展Activity,而是扩展了Broadcast Receiver。这是DisplayNotification的onReceive方法:
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "DisplayNotification onReceive");
int notifID = intent.getExtras().getInt("NotifID");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("NotifID", notifID);
PendingIntent detailsIntent = PendingIntent.getActivity(context, 0, i, 0);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "See the quote of the day!", System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
CharSequence from = "Notification";
CharSequence message = "See the activity!";
notif.setLatestEventInfo(context, from, message, detailsIntent);
nm.notify(notifID, notif);
}
答案 0 :(得分:2)
我读完整个问题2-3次..
现在我想指出那里的一些事情,
您已在Activity C
创建通知,将点击 调用MainActivity。
但是,当您从B 设置闹钟时,您正在通过intent of Activity C
,虽然您的C
代码没有 {{1在onCreate()的末尾你有写完(),所以当通过B的报警集调用Activity C时,它只调用Activity C并设置Notification ..but当您编写完成()时,它将结束活动C并从堆栈中显示最新的活动B 。
如果你想交叉检查.. 在活动C的onCreate(),onPause()和onDestroy()中写入日志 ..您将看到在触发警报时创建活动C并销毁。
解决方案:
如果您在主屏幕和警报触发时不想显示任何活动,那么,
在活动B中,您为活动C编写了待处理的意图。您将需要更改以呼叫广播接收器并使用setContentView
其PendingIntent.getBroadcast
和您拥有的代码用C语言写的通知你必须在Receiver中写这个 ...因此,当触发警报时,你的活动都不会被调用。
快乐编码:)