从BroadcastReceiver启动活动

时间:2014-09-12 15:24:50

标签: java android

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

   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?"); 
      }
   }
通过onclick

logcat开始活动:

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 

1 个答案:

答案 0 :(得分:1)

onRecieve()方法旁边使用

context.startActivity(yourintent);

此处 context onRecieve()方法中作为参数传递。

您必须像往常一样创建yourintent

但你可能需要设置一个标志,意图如下:

yourintent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK)