1)我需要在警报触发时启动一个活动,我尝试在BroadCastReceiver的onRecieve事件中添加活动但是它失败了。
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);
}
}
错误:
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)即使按下后退按钮,如何让活动继续运行。当满足源代码中的某些条件时,活动将关闭。例如警报不会响起,他也无法关闭应用程序,直到用户正确回答问题。
答案 0 :(得分:3)
将下面的代码写入您的BroadcastReceiver类。
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);
而不是
Activity act = new Activity();
act.startActivity(intent);
将Receiver类定义到androidmanifest.xml文件中。
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="de.vogella.android.mybroadcast" />
</intent-filter>
</receiver>
答案 1 :(得分:2)
至于第二部分,请按
覆盖BackButton@Override
public void onBackPressed() {
//do nothing or whatever you would want to display
return;
}
要覆盖“菜单”和“主页”按钮,请使用以下代码..
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);
}
答案 2 :(得分:2)
修改onReceive方法
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);
}