gpt4 book ai didi

来自类的 Android Intent With On Receive

转载 作者:行者123 更新时间:2023-11-29 01:59:09 24 4
gpt4 key购买 nike

我有一个扩展 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 方法而不是在 Intent 中启动 Activity 时都会崩溃。

我几乎尝试了一切。

如有任何帮助,我们将不胜感激。

谢谢

编辑: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

最佳答案

测试消息不为空,否则您将遇到 nullPointerException。另外,如果您不在 Activity 中使用它,则需要:

intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK);

否则你也会崩溃。

关于来自类的 Android Intent With On Receive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com