Broadcast Receiver for ACTION_USER_PRESENT,ACTION_

2019-04-29 00:43发布

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.

5条回答
萌系小妹纸
2楼-- · 2019-04-29 00:51

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

查看更多
够拽才男人
3楼-- · 2019-04-29 00:52

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

查看更多
欢心
4楼-- · 2019-04-29 00:53

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.

查看更多
狗以群分
5楼-- · 2019-04-29 00:55

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.

查看更多
Luminary・发光体
6楼-- · 2019-04-29 00:56

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>
查看更多
登录 后发表回答