我在 AndroidManifest.xml 中定义了一个 BroadCastReceiver,如下所示
<receiver
android:name="com.example.hello.ScreenUnlockReceiver"
android:enabled="true"
android:singleUser="true">
<intent-filter>
<action android:name="android.content.Intent.ACTION_USER_PRESENT" />
</intent-filter>
</receiver>
并定义接收器如下:
public class ScreenUnlockReceiver
extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//start activity
Intent i = new Intent();
i.setClassName("com.example.hello", "LoginActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
但是当我解锁屏幕并且没有显示 LoginActivity 时,广播接收器没有被触发。 LoginActivity是android sdk自带的默认loginactivity。
我是否缺少使用权限或其他内容,请告诉我。 谢谢
桑托斯
最佳答案
你必须拦截的 Action 是:
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
关于android - BroadcastReceiver onReceive 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16357172/