嗨,我试图从 ScreenReceiver 类调用我的 mainactivity 中的方法。我发现了这一点并将其实现到我的代码中。
主要活动:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ScreenRecv
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
// Toast(String)
public void Toast(String text) {
Toast.makeText(MainActivity.this, "" + text, Toast.LENGTH_SHORT).show();
}
}
屏幕接收器:
package com.wifitimer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
new MainActivity().Toast("Screen Off");
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
new MainActivity().Toast("Screen On");
wasScreenOn = true;
}
}
}
我没有收到任何错误,但 Toast 也没有显示。有任何想法吗?
日志猫:
04-16 00:53:35.470: I/WifiManager(19123): setWifiEnabled : true
04-16 00:53:57.135: D/AndroidRuntime(19123): Shutting down VM
04-16 00:53:57.135: W/dalvikvm(19123): threadid=1: thread exiting with uncaught exception (group=0x416922a0)
04-16 00:53:57.140: E/AndroidRuntime(19123): FATAL EXCEPTION: main
04-16 00:53:57.140: E/AndroidRuntime(19123): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.SCREEN_OFF flg=0x50000010 } in com.wifitimer.ScreenReceiver@42312238
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.os.Handler.handleCallback(Handler.java:615)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.os.Looper.loop(Looper.java:137)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.app.ActivityThread.main(ActivityThread.java:4898)
04-16 00:53:57.140: E/AndroidRuntime(19123): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 00:53:57.140: E/AndroidRuntime(19123): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 00:53:57.140: E/AndroidRuntime(19123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-16 00:53:57.140: E/AndroidRuntime(19123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-16 00:53:57.140: E/AndroidRuntime(19123): at dalvik.system.NativeStart.main(Native Method)
04-16 00:53:57.140: E/AndroidRuntime(19123): Caused by: java.lang.NullPointerException
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
04-16 00:53:57.140: E/AndroidRuntime(19123): at com.wifitimer.MainActivity.Toast(MainActivity.java:107)
04-16 00:53:57.140: E/AndroidRuntime(19123): at com.wifitimer.ScreenReceiver.onReceive(ScreenReceiver.java:13)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
04-16 00:53:57.140: E/AndroidRuntime(19123): ... 9 more
04-16 00:53:57.445: I/Process(19123): Sending signal. PID: 19123 SIG: 9