从活动之外调用startActivity()? [英] Calling startActivity() from outside of an Activity?
问题描述
我使用的是 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屋!