Reputation: 2203
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.
Upvotes: 1
Views: 5013
Reputation: 143
Modify your onReceive Method
Intent intent;
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "Recieved!!", Toast.LENGTH_LONG).show();
intent = new Intent();
intent.setClass(context, Test.class); //Test is a dummy class name where to redirect
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("msg", str);
context.startActivity(intent);
}
Upvotes: 2
Reputation: 22291
Write below Code into your BroadcastReceiver class.
Intent i = new Intent();
i.setClassName("your package name", "your package name.your activity name");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
instead of
Activity act = new Activity();
act.startActivity(intent);
Define Receiver class into your androidmanifest.xml file.
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="de.vogella.android.mybroadcast" />
</intent-filter>
</receiver>
Upvotes: 3
Reputation: 16354
As for the second part, override the BackButton by
@Override
public void onBackPressed() {
//do nothing or whatever you would want to display
return;
}
To override the Menu and the Home button, use the following code..
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
//Toast.makeText(this, "You pressed the Home button!", Toast.LENGTH_LONG).show();
return true; // event has been handled
}
if(keyCode == KeyEvent.KEYCODE_MENU)
{
//Toast.makeText(this, "You pressed the Menu button!", Toast.LENGTH_LONG).show();
return true; // tell your phone that you have handled the event
}
return super.onKeyDown(keyCode, event);
}
Upvotes: 2