【问题标题】:Broadcast Receiver for ACTION_USER_PRESENT,ACTION_SCREEN_ON,ACTION_BOOT_COMPLETEDACTION_USER_PRESENT、ACTION_SCREEN_ON、ACTION_BOOT_COMPLETED 的广播接收器
【发布时间】:2011-10-01 10:40:55
【问题描述】:

我正在创建一个使用广播接收器的类。我想在手机解锁时接收广播。但是有一些问题。请帮帮我。

我的 Manifest.xml 是:-

<receiver android:name=".MyReciever">
    <intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.ACTION_USER_PRESENT" />
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
            <action android:name="android.intent.action.ACTION_SCREEN_ON" />
        </intent-filter>
    </intent-filter>
</receiver>

和我的广播接收器类:-

public class MyReiever extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
   Log.d("My Reciever","is intent null => " + (intent == null));
   Log.d("My Reciever",intent.getAction()+"");
   }
}

虽然其他应用程序和服务正在接收“Screen_on”和“USer_Present”的广播,例如。 Wifi 服务。

【问题讨论】:

  • 我的清单文件中有错误。正确的清单如下:- &lt;receiver android:name=".MyReciever"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.ACTION_USER_PRESENT" /&gt; &lt;action android:name="android.intent.action.ACTION_BOOT_COMPLETED" /&gt; &lt;action android:name="android.intent.action.ACTION_SCREEN_ON" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt;

标签: java android xml broadcastreceiver android-manifest


【解决方案1】:

虽然 Java 常量是 android.content.intent.ACTION_USER_PRESENTandroid.content.intent.ACTION_BOOT_COMPLETEDandroid.content.intent.ACTION_SCREEN_ON,但这些常量的android.intent.action.USER_PRESENTandroid.intent.action.BOOT_COMPLETEDandroid.intent.action.SCREEN_ON。正是这些值需要出现在您的清单中。

但是请注意,ACTION_SCREEN_ON 的接收者不能在清单中声明,而必须通过 Java 代码注册,例如参见 this question

【讨论】:

    【解决方案2】:

    由于隐式广播接收器在 Android 8.0 中无法正常工作,因此您必须通过代码以及在清单中注册您的接收器。

    执行以下步骤:

    • 添加清单标签
    <receiver android:name=".MyReciever">
        <intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.ACTION_USER_PRESENT" />
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
                <action android:name="android.intent.action.ACTION_SCREEN_ON" />
            </intent-filter>
        </intent-filter>
    </receiver>
    
    • 创建一个接收器类并添加您的代码
    public class MyReciever extends BroadcastReceiver {
    
       @Override
       public void onReceive(Context context, Intent intent) {
       Log.d("My Reciever","is intent null => " + (intent == null));
       Log.d("My Reciever",intent.getAction()+"");
       }
    }
    
    • 创建服务并在其中注册接收器
    public class MyService extends Service {
    MyReceiver receiver = new MyReceiver();
    
        @Override
        public IBinder onBind(Intent intent) { return null; }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            registerReceiver(receiver);
            return START_STICKY;
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            unregisterReceiver(receiver);
        }
    }
    
    • 别忘了在清单中定义服务
    <service android:name=".MyService"/>
    

    要使您的广播正常运行,您必须在服务中注册它。为了让您的服务保持活力,您可以使用与此问题无关的工具,如 alarm managersjobs 等。

    【讨论】:

    • 能否请您发布完整的解决方案。 @马尔夫
    • @NoumanCh 这是一个完整的代码,但我想我失去了赏金。对吗?
    • 不幸的是。
    • 感谢您的回答,@Mahdi-Malv。但是有一个问题:截至 2020 年 2 月,registerReceiver 需要两个参数:receiverintent
    • @kittykittybangbang 我想你的意思是IntentFilter。我没有找到需要 Intent 的新 API,你能提供参考吗?
    【解决方案3】:

    检查您的类名,即扩展广播接收器。应该是“MyReciever”而不是“MyReiever”

    【讨论】:

      【解决方案4】:

      除了前面答案中提到的 Typo 以及接收器类包名称应该在receiver 标记中完全提及这一事实之外,我认为问题在于有问题的代码使用了两个嵌套的意图过滤器。我相信这段代码会正常工作:

       <receiver android:name=".MyReciever">
          <intent-filter>
                  <action android:name="android.intent.action.ACTION_USER_PRESENT" />
                  <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
                  <action android:name="android.intent.action.ACTION_SCREEN_ON" />
          </intent-filter>
      </receiver>
      

      【讨论】:

        【解决方案5】:

        我只能给你一个快速提示,因为我已经走过了你所遵循的道路,但成功率要低得多。尝试使用 java.util.logging 读取 logcat,这样您就不需要读取日志的权限。并在日志视图中为包含“系统禁用”作为其标题的监听器创建监听器。它在锁定和解锁时都会启动。检查允许访问 system.android 而不是另一个屏幕的那个。 希望能帮助到你。祝你好运

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多