android - BroadcastReceiver onReceive 没有被调用

标签 android broadcastreceiver

我在 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>

Developer Android ACTION_USER_PRESENT

关于android - BroadcastReceiver onReceive 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16357172/

相关文章:

android - 广播接收器

android - 如何在 RxJava 中刷新 observable?

java - 当我第二次将数据从另一个 Activity 发送到 MainActivity 时,我得到的数据是我第一次得到的

java - 如何使 TextView 不可见

android - 如何在 Eclipse 中运行 Android 应用程序?

android - Xamarin.Android:在 SMS Delivered Broadcast Receiver 中检测 SMS

android - 如何在不对已经支持智能卡 API 的设备进行 root 的情况下访问安全元素

android - 在 Android 中获取我调用的号码

android - 如何修复 Android 错误 "Background execution not allowed: receiving Intent {...}"

android - 短信广播接收器在一天后未触发