当用户在我的通知中单击一个按钮时,我试图打开MainActivity
,而应用程序仅在后台运行,并提供服务.单击按钮时,Service
类中会触发以下行:
Intent openApp = new Intent(this, MainActivity.class);
openApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openApp);
我已经判断过了,线被触发了,所以对按钮的点击做出react 没有问题,但是Activity
不会打开.
有什么建议吗?为什么这对我不起作用,我怎样才能让它起作用?
编辑
我被要求提供更多的代码,所以在我的Service
中的onStartCommand()
中,如果它以intent
中的停止操作开始,我调用killService()
方法,它杀死Service
,启动MainActivity
,并做一些其他事情:
if (action != null && action.equals(ACTION_STOP_SERVICE)) {
killService();
}
要设置Notification
s按钮,我使用以下代码:
Intent stopActionIntent = new Intent(this, TimerService.class);
stopActionIntent.setAction(ACTION_STOP_SERVICE);
PendingIntent stopActionPendingIntent = PendingIntent.getService(this, 1, stopActionIntent, PendingIntent.FLAG_IMMUTABLE);
timerNotificationBuilder.addAction(R.drawable.stop, "Stop", stopActionPendingIntent);
正如我所说,这个按钮已经对用户点击它做出了react ,所以这不是问题所在.