ACTION_USER_PRESENT、ACTION_SCREEN_ON、ACTION_BOOT_COMPLETED 的广播接收器

发布于 2024-12-07 06:51:29 字数 893 浏览 0 评论 0原文

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

我的 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”的广播,例如。无线网络服务。

I am creating a class which uses broadcast receiver. I want to receive the broadcast on unlocking of the phone. But there is some issue. Please help me out.

My Manifest.xml is :-

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

and my Broadcast reciever class :-

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()+"");
   }
}

Though other application and services are receiving broadcast for "Screen_on" and "USer_Present" eg. WifiService.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

§普罗旺斯的薰衣草 2024-12-14 06:51:29

尽管 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 代码注册,请参阅例如 这个问题

Although the Java constants are android.content.intent.ACTION_USER_PRESENT, android.content.intent.ACTION_BOOT_COMPLETED, and android.content.intent.ACTION_SCREEN_ON, the values of those constants are android.intent.action.USER_PRESENT, android.intent.action.BOOT_COMPLETED, and android.intent.action.SCREEN_ON. It is those values which need to appear in your manifest.

Note, however, that a receiver for ACTION_SCREEN_ON can not be declared in a manifest but must be registered by Java code, see for example this question.

呢古 2024-12-14 06:51:29

由于隐式广播接收器从 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"/>

要使广播正常工作,您必须在服务中注册它。为了保持您的服务活跃,您可以使用与此问题无关的警报管理器作业等工具。

Since implicit broadcast receivers are not working as of Android 8.0, you must register your receiver by code and also in the manifest.

Do these steps:

  • Add manifest tag
<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>
  • Create a receiver class and add your codes
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()+"");
   }
}
  • Create a service and register the receiver in it
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);
    }
}
  • Don't forget to define the service in manifest
<service android:name=".MyService"/>

To make your broadcast work, you have to register it in service. And to keep your service alive you can use tools like alarm managers, jobs, etc which is not related to this question.

热情消退 2024-12-14 06:51:29

检查你的类名,它正在扩展BroadcastReceiver。它应该是“MyReciever”而不是“MyReciever”

Check your Class name, that is extending BroadcastReceiver. It should be "MyReciever" not "MyReiever"

香橙ぽ 2024-12-14 06:51:29

除了前面的答案中提到的打字错误以及接收器类包名称应该在 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>

Beside Typo that mentioned in earlier answers and the fact that the receiver class package name should be completely mentioned in receiver tag, I think the problem is that the code in question uses two nested intent filters. I believe this code will work correctly:

 <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>
厌味 2024-12-14 06:51:29

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

I can only give you a quick tip as i have gone through that path you are following with much less success. Try to read logcat using java.util.logging so that you will not require permission to read logs. And in log view create listener for the one containing "system disable" as its header. it fires up both at lock and unlock. check for the one that gives access to system.android not the other screen.
Hope it helps. Best of luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文