Android 从注册了应用程序上下文的braodcast接收器启动活动
我想从广播接收器启动一个活动,用于定制意图。我不想要那个活动的多个实例,所以我不使用newtask标志 我已经在舱单上登记了这些接收人。我犯了一个错误Android 从注册了应用程序上下文的braodcast接收器启动活动,android,broadcastreceiver,Android,Broadcastreceiver,我想从广播接收器启动一个活动,用于定制意图。我不想要那个活动的多个实例,所以我不使用newtask标志 我已经在舱单上登记了这些接收人。我犯了一个错误 "03-02 12:54:58.634: W/System.err(3763): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TAS
"03-02 12:54:58.634: W/System.err(3763): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?"
我应该在哪里注册接收者?尝试在活动中注册时,接收者可以启动活动。在清单中注册时可能会出现什么问题?请使用以下代码
public void onReceive(Context context, Intent intent)
{
System.out.println ( "Application Started" );
// put your TimerTask calling class here
try
{
Intent myIntent = new Intent ( context, AutoStartExampleActivity.class );
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
catch ( Exception e )
{
System.out.println ( " Error while Starting Activity " + e.toString() );
}
}
请使用以下代码
public void onReceive(Context context, Intent intent)
{
System.out.println ( "Application Started" );
// put your TimerTask calling class here
try
{
Intent myIntent = new Intent ( context, AutoStartExampleActivity.class );
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
catch ( Exception e )
{
System.out.println ( " Error while Starting Activity " + e.toString() );
}
}
在android清单文件中注册接收者
<receiver android:name="com.example.XYZReceiver"></receiver>
在android清单文件中注册接收者
<receiver android:name="com.example.XYZReceiver"></receiver>
这是因为
活动
覆盖了上下文
的startActivity()
方法,因此如果在活动中调用startActivity()
,将不会出现问题,但是如果从上下文
调用它,您应该将flagflag\u ACTIVITY\u NEW\u TASK
添加到意图中。这是因为活动
覆盖了上下文
的startActivity()
方法,因此如果您在活动中调用startActivity()
,将不会出现问题,但是如果您从上下文
调用它,您应该将flagflag\u ACTIVITY\u NEW\u TASK
添加到意图中