0

我试图在屏幕解锁时接收事件 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>

日志猫

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
4

0 回答 0