我正在尝试在屏幕解锁时收到事件ACTION_USER_PRESENT。我有一个注册的广播接收器,它应该在屏幕打开时给出一条日志消息,然后解锁。它会显示屏幕上的消息,但会在屏幕解锁期间(用户在场)使应用程序崩溃。
有什么想法吗?我没有运气多次搜索过这个问题。顺便提一下,我在ICS 4.0+上并在多个设备上进行了测试。
这是我的广播
@Override
public void onReceive(Context context, Intent intent) {
Log.d("BRODCASTRECEIVER", "MAIN");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
Log.d("BRODCASTRECEIVER", "SCREEN OFF");
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
Log.d("BRODCASTRECEIVER", "SCREEN ON");
}
else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.d("BRODCASTRECEIVER", "UNLOCK");
}
}
};
以下是我注册的方式
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_USER_PRESENT);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(Receiver, intentFilter);
这是清单
<receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
logcat的
12-09 12:39:45.582: E/AndroidRuntime(12710): FATAL EXCEPTION: main
12-09 12:39:45.582: E/AndroidRuntime(12710): java.lang.RuntimeException: Unable to instantiate receiver jp.dip.sugarhouse.lockscreenoverlay.MyBroadcastReceiver: java.lang.ClassNotFoundException: jp.dip.sugarhouse.lockscreenoverlay.MyBroadcastReceiver
12-09 12:39:45.582: E/AndroidRuntime(12710): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2100)
12-09 12:39:45.582: E/AndroidRuntime(12710): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
12-09 12:39:45.582: E/AndroidRuntime(12710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
12-09 12:39:45.582: E/AndroidRuntime(12710): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 12:39:45.582: E/AndroidRuntime(12710): at android.os.Looper.loop(Looper.java:137)
12-09 12:39:45.582: E/AndroidRuntime(12710): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-09 12:39:45.582: E/AndroidRuntime(12710): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 12:39:45.582: E/AndroidRuntime(12710): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 12:39:45.582: E/AndroidRuntime(12710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
12-09 12:39:45.582: E/AndroidRuntime(12710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
12-09 12:39:45.582: E/AndroidRuntime(12710): at dalvik.system.NativeStart.main(Native Method)
12-09 12:39:45.582: E/AndroidRuntime(12710): Caused by: java.lang.ClassNotFoundException: jp.dip.sugarhouse.lockscreenoverlay.MyBroadcastReceiver
12-09 12:39:45.582: E/AndroidRuntime(12710): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-09 12:39:45.582: E/AndroidRuntime(12710): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-09 12:39:45.582: E/AndroidRuntime(12710): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-09 12:39:45.582: E/AndroidRuntime(12710): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2095)
12-09 12:39:45.582: E/AndroidRuntime(12710): ... 10 more