gpt4 book ai didi

java - 从 BroadcastReceiver 启动 Activity

转载 作者:行者123 更新时间:2023-11-30 02:40:36 25 4
gpt4 key购买 nike

我使用了 BroadcastReceiver,它每 10 秒发出一次警报,在这里当它发出警报时它会启动我的应用程序或我的应用程序的 Activity 。我如何创建该发布?

   public void onReceive(Context context, Intent intent) { 
generateNotification(context,"Hi how are you?");
String action = intent.getAction();
Intent yourintent = new Intent(null, OutPut.class);
yourintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(yourintent);
if (SOMEACTION.equals(action)) {
generateNotification(context,"Hi how are you?");
}
}

logcat 通过 onclick 启动 Activity :

by onclick it returns :
09-12 12:00:54.750: E/AndroidRuntime(2251): FATAL EXCEPTION: main
09-12 12:00:54.750: E/AndroidRuntime(2251): Process: com.manish.alarmmanager, PID: 2251
09-12 12:00:54.750: E/AndroidRuntime(2251): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manish.alarmmanager/com.manish.alarmmanager.OutPut}: java.lang.NullPointerException
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.app.ActivityThread.access$700(ActivityThread.java:135)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.os.Handler.dispatchMessage(Handler.java:102)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.os.Looper.loop(Looper.java:137)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.app.ActivityThread.main(ActivityThread.java:4998)
09-12 12:00:54.750: E/AndroidRuntime(2251): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 12:00:54.750: E/AndroidRuntime(2251): at java.lang.reflect.Method.invoke(Method.java:515)
09-12 12:00:54.750: E/AndroidRuntime(2251): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-12 12:00:54.750: E/AndroidRuntime(2251): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
09-12 12:00:54.750: E/AndroidRuntime(2251): at dalvik.system.NativeStart.main(Native Method)
09-12 12:00:54.750: E/AndroidRuntime(2251): Caused by: java.lang.NullPointerException
09-12 12:00:54.750: E/AndroidRuntime(2251): at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
09-12 12:00:54.750: E/AndroidRuntime(2251): at com.manish.alarmmanager.OutPut.onCreate(OutPut.java:21)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.app.Activity.performCreate(Activity.java:5243)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-12 12:00:54.750: E/AndroidRuntime(2251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
09-12 12:00:54.750: E/AndroidRuntime(2251): ... 11 more

最佳答案

onRecieve() 方法中使用

context.startActivity(yourintent);

这里 context 作为参数传递给 onRecieve() 方法。

你必须像往常一样创建yourintent

但您可能必须按如下方式设置一个 intent 标志:

yourintent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK)

关于java - 从 BroadcastReceiver 启动 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25811700/

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