android: how to call activity by clicking foreground service notification

随声附和 提交于 2019-12-22 12:26:40

问题


I start a foreground service which shows a notification. If my activity is hidden I want it start by clicking on the notification. A function called in onStartCommand does this:

startForeground(noti_id, mNoti);

The notification appears and works but it doesn't reactivate my MainActivity:

notiIntent = new Intent(this, MainGate.class);
notiPendingIntent = PendingIntent.getActivity(this, 0, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);

MainGate.class is the activity which starts the foreground service. It should appear when I click on the notification.

EDIT:

Actually, it worked when the notification was built in the man activity (MainGate.class). And it worked when notification was built in the service not being foreground service. Now I had to implement the foreground service and it stopped working.


回答1:


try this solution

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


来源:https://stackoverflow.com/questions/40511751/android-how-to-call-activity-by-clicking-foreground-service-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!