如何使用意图操作USER_PRESENT?

sam*_*uke 11 android broadcastreceiver android-intent

我有一个时钟小部件应用程序,我需要识别手机何时解锁,我相信我可以使用动作USER_PRESENT,但我不能让它在BroadcastReceiver类中启动,我将它设置在表现如下:

    <receiver
        android:name="com.myApp.myApp.MyWidgetIntentReceiver"
        android:exported="false"
        android:label="widgetBroadcastReceiver" >
        <intent-filter> 
            <action android:name="android.intent.action.USER_PRESENT" >
            </action>                               
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/demo_widget_provider" />
    </receiver>
Run Code Online (Sandbox Code Playgroud)

这就是我试图在BroadcastReceiver中获取它的方式:

public class MyWidgetIntentReceiver extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT){
            Log.i("TICK", intent.getAction());          
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我解锁手机后没有开火,你能帮我解决一下,还是给我一个更好的方法来检查手机什么时候解锁?谢谢!

sha*_*ane 7

消除android:exported="false"

\n\n
\n

安卓:导出:

\n
\n\n

广播接收器是否可以从其应用程序外部的源接收消息 \xe2\x80\x94 如果可以,则为“true”,如果不能,则为“false”。如果为“假”,则广播接收器可以接收的唯一消息是由同一应用程序或具有相同用户 ID 的应用程序的组件发送的消息。

\n\n

来源:developer.android.com

\n


One*_*rld 5

消除android:exported="false"。这对我在 Stock Android 5 上有用


sam*_*uke 2

我通过在 AppWidgetProvider 类的 onUpdate 方法中使用registerReceiver并传递 BroadcastReceiver 类的实例来注册 Intent.ACTION_USER_PRESENT 来使其工作,因为仅将其添加到清单中并没有执行任何操作。谢谢你!