从活动外部调用 startActivity()? [英] Calling startActivity() from outside of an Activity?

查看:23
本文介绍了从活动外部调用 startActivity()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AlarmManager 来触发广播信号的意图.以下是我的代码:

I'm using an AlarmManager to trigger an intent that broadcasts a signal. The following is my code:

AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, Wakeup.class);
try
{
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
    Long elapsed +=  // sleep time;
    mgr.set(AlarmManager.RTC_WAKEUP, elapsed, pi);
}
catch(Exception r)
{
    Log.v(TAG, "RunTimeException: " + r);
}

我从 Activity 调用此代码,所以我不知道我怎么会收到以下错误...

I'm calling this code from an Activity, so I don't know how I could be getting the following error...

ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

推荐答案

如果你的 android 版本低于 Android - 6 那么你需要添加这一行,否则它会在 Android - 6 以上运行.

if your android version is below Android - 6 then you need to add this line otherwise it will work above Android - 6.

...
Intent i = new Intent(this, Wakeup.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...

这篇关于从活动外部调用 startActivity()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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