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

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

问题描述

我使用的是 AlarmManager 来触发广播信号的意图。以下是我的code:

  AlarmManager经理=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
意图I =新的意图(这一点,Wakeup.class);
尝试
{
    PendingIntent圆周率= PendingIntent.getBroadcast(此,0,I,0);
    龙经过+ = //睡眠时间;
    mgr.set(AlarmManager.RTC_WAKEUP,过去,PI);
}
赶上(例外R)
{
    Log.v(TAG的RuntimeException:+ R);
}
 

我打电话从活动这code,所以我不知道我怎么会收到以下错误...

  

ERROR / AndroidRuntime(7557):java.lang.RuntimeException的:无法启动接收器com.wcc.Wakeup:android.util.AndroidRuntimeException:从活动背景以外调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。难道这真的是你想要的吗?

解决方案

如果你加入这一行:

  ...
意图I =新的意图(这一点,Wakeup.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...
 

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

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?

解决方案

What if you add this line:

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

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

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