我启动显示通知的前台服务。如果我的活动是隐藏的,我希望它通过单击通知开始。
在onStartCommand中调用的函数可以执行以下操作:
startForeground(noti_id, mNoti);
该通知会出现并起作用,但不会重新激活我的MainActivity:
notiIntent = new Intent(this, MainGate.class);
notiPendingIntent = PendingIntent.getActivity(this, 0, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
MainGate.class是启动前台服务的活动。当我单击通知时,它应该出现。
编辑:
实际上,当在man活动(MainGate.class)中构建通知时,它就起作用了。当通知不是在前台服务中构建的,它就可以正常工作。现在,我必须实现前台服务,并且它停止工作。
最佳答案
试试这个解决方案
Intent notificationIntent = new Intent(this, MainGate.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText(getString(R.string.isRecording))
.setContentIntent(pendingIntent).build();
startForeground(NOTIFICATION_ID, notification);