“startactivity非活动方面的服务被称为”警告通知,从启动时的意图 [英] 'startactivity called from non-activity context service' warning when launching intent from notification

查看:1344
本文介绍了“startactivity非活动方面的服务被称为”警告通知,从启动时的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务这将启动的通知 startForeground(),我想通知发动对点击的活动。结果
acitivty 我要启动定义为的android:launchMode =singleTask,通常在服务启动之前运行<。 BR>
这里是我的未决意图创建code:

I've a service which starts a notification with startForeground(), I want the notification to launch an activity on click.
The acitivty I want to launch defined as android:launchMode="singleTask" and usually runs before the service starts.
Here is my pending intent creation code :

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

当我点击通知我得到以下警告:

When I click on the notification I get the following warning :

startActivity called from non-Activity context; 
forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent........

我也试着让活动与这个而不是的getApplicationContext(),但得到了同样的警告。结果
我应该怎样做这个吧?

I've also tried getting the activity with this instead of getApplicationContext() but got the same warning.
How should I make this right ?

推荐答案

不要使用 Intent.FLAG_ACTIVITY_NEW_TASK PendingIntent.getActivity 。更好地利用 FLAG_ONE_SHOT

Don't use Intent.FLAG_ACTIVITY_NEW_TASK for PendingIntent.getActivity. Better use FLAG_ONE_SHOT.

,并尝试使用上下文 活动的而不是 getApplicationContext()

And try to use Context of Activity instead getApplicationContext().

这篇关于“startactivity非活动方面的服务被称为”警告通知,从启动时的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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