1

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)即使按下后退按钮,我如何使活动继续运行。当满足源代码中的某些条件时,活动将关闭。例如警报不会响起,他也无法关闭应用程序,直到用户正确回答问题。

4

3 回答 3

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>
于 2012-06-22T09:50:26.490 回答
2

至于第二部分,通过

@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);
    }
于 2012-06-22T09:51:38.173 回答
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);
}
于 2012-06-22T10:15:17.197 回答