android:如何通过单击前台服务通知来调用活动 [英] android: how to call activity by clicking foreground service notification

查看:56
本文介绍了android:如何通过单击前台服务通知来调用活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启动了一个显示通知的前台服务.如果我的活动被隐藏,我希望它通过点击通知开始.在 onStartCommand 中调用的函数执行此操作:

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

通知出现并起作用,但它不会重新激活我的 MainActivity:

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 是启动前台服务的活动.当我点击通知时它应该会出现.

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

实际上,当在 man 活动 (MainGate.class) 中构建通知时,它就起作用了.当通知是在不是前台服务的服务中构建时,它就起作用了.现在我必须实现前台服务,但它停止工作.

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.

推荐答案

试试这个解决方案

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

这篇关于android:如何通过单击前台服务通知来调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆