Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从注册了应用程序上下文的braodcast接收器启动活动_Android_Broadcastreceiver - Fatal编程技术网

Android 从注册了应用程序上下文的braodcast接收器启动活动

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

我想从广播接收器启动一个活动,用于定制意图。我不想要那个活动的多个实例,所以我不使用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_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()
,将不会出现问题,但是如果从
上下文
调用它,您应该将flag
flag\u ACTIVITY\u NEW\u TASK
添加到意图中。

这是因为
活动
覆盖了
上下文
startActivity()
方法,因此如果您在活动中调用
startActivity()
,将不会出现问题,但是如果您从
上下文
调用它,您应该将flag
flag\u ACTIVITY\u NEW\u TASK
添加到意图中