Android:如何通过点击前台服务通知来调用活动

3

我启动了一个前台服务,它会显示一个通知。如果我的活动被隐藏了,我希望通过点击通知来启动它。 在onStartCommand中调用的一个函数可以实现这个功能:

startForeground(noti_id, mNoti);

通知出现并且工作了,但它没有重新激活我的MainActivity。
notiIntent = new Intent(this, MainGate.class);
notiPendingIntent = PendingIntent.getActivity(this, 0, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);

MainGate.class是启动前台服务的活动。当我点击通知时,它应该出现。

编辑:

实际上,当通知在主要活动(MainGate.class)中构建时,它可以工作。并且当通知在不是前台服务的服务中构建时,它也可以工作。现在我必须实现前台服务,但它停止工作了。

1个回答

3

尝试这个解决方案

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接