我有一个应用程序,它有一个MainActivity(Mr.Obvious),一个服务和一个BroadcastReceiver。在我的MainActivity中,我有一个像这样的AlarmManager:
Intent myIntent = new Intent(MainActivity.this, BroadReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_DAY, pendingIntent);
在我的BroadcastReceiver中,我有这个:
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotifyService.class);
context.startService(service1);
}
在我的服务中,我有这个:
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationManager notificationManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new NotificationCompat.Builder(this)
.setTicker(getText(R.string.notification_ticker))
.setSmallIcon(R.drawable.green)
.setContentTitle(frase())
.setContentText(getText(R.string.notification_text))
.setAutoCancel(true)
.build();
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationManager.notify(0, notification);}
我希望我的应用在用户收到通知时启用MainActivity中的按钮,可能吗?
答案 0 :(得分:0)
- 如果您在后台开始新活动
Intent in = new Intent(this, MainActivity.class);
in.putExtra("state", "background");
startActivity(in);
获取额外字符串然后比较它并控制按钮
如果您在前台将按钮对象设为公共,则控制它
使用全局变量识别前景中的MainActivity(使用活动生命周期)
onDistroy()
onStart()