1) I need to launch an activity when the alarm triggers , I tried add the activity when in the onRecieve event of BroadCastReceiver but its failing.
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "Recieved!!", Toast.LENGTH_LONG).show();
Activity act = new Activity();
act.startActivity(intent);
}
}
Error:
06-22 13:42:00.733: W/dalvikvm(750): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-22 13:42:00.763: E/AndroidRuntime(750): FATAL EXCEPTION: main
06-22 13:42:00.763: E/AndroidRuntime(750): java.lang.RuntimeException: Unable to start receiver com.exercise.AndroidTime.AlarmReceiver: java.lang.NullPointerException
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.os.Looper.loop(Looper.java:123)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-22 13:42:00.763: E/AndroidRuntime(750): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 13:42:00.763: E/AndroidRuntime(750): at java.lang.reflect.Method.invoke(Method.java:507)
06-22 13:42:00.763: E/AndroidRuntime(750): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-22 13:42:00.763: E/AndroidRuntime(750): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-22 13:42:00.763: E/AndroidRuntime(750): at dalvik.system.NativeStart.main(Native Method)
06-22 13:42:00.763: E/AndroidRuntime(750): Caused by: java.lang.NullPointerException
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.Activity.startActivityForResult(Activity.java:2827)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.Activity.startActivity(Activity.java:2933)
06-22 13:42:00.763: E/AndroidRuntime(750): at com.exercise.AndroidTime.AlarmReceiver.onReceive(AlarmReceiver.java:23)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
06-22 13:42:00.763: E/AndroidRuntime(750): ... 10 more
2) How do I make an activity keep running even though the back button is pressed. Activity will close when certain criteria in the source code is met. Such as the alarm will not go off and he cant close the app also until user answers a question correctly.
Write below Code into your BroadcastReceiver class.
instead of
Define Receiver class into your androidmanifest.xml file.