2

我有一个扩展 BroadCastReceiever 的类,并且 onReceive 方法如下:

public class Alarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String message = intent.getStringExtra("message2");
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        Intent intent1 = new Intent(context, AlarmShow.class);
        context.startActivity(intent1);

    }
}

但是它每次运行 onReceive 方法而不是在意图中启动活动时都会崩溃。

我几乎什么都试过了。

任何帮助,将不胜感激。

谢谢

编辑:LogCat 错误:

11-25 18:24:00.511: D/AndroidRuntime(2158): Shutting down VM
11-25 18:24:00.511: W/dalvikvm(2158): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-25 18:24:00.521: E/AndroidRuntime(2158): FATAL EXCEPTION: main
11-25 18:24:00.521: E/AndroidRuntime(2158): java.lang.RuntimeException: Unable to start receiver com.example.androidalarm.Alarm: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.os.Looper.loop(Looper.java:123)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at java.lang.reflect.Method.invoke(Method.java:507)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at dalvik.system.NativeStart.main(Native Method)
11-25 18:24:00.521: E/AndroidRuntime(2158): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.app.ContextImpl.startActivity(ContextImpl.java:621)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at com.example.androidalarm.Alarm.onReceive(Alarm.java:20)
11-25 18:24:00.521: E/AndroidRuntime(2158):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
11-25 18:24:00.521: E/AndroidRuntime(2158):     ... 10 more
11-25 18:24:02.611: I/Process(2158): Sending signal. PID: 2158 SIG: 9
4

1 回答 1

1

测试消息不为空,否则您将遇到 nullPointerException。此外,如果您没有在活动中使用它,您需要:

intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK);

否则你也会遇到崩溃。

于 2012-11-25T23:24:07.920 回答