Can an ACTION_USER_PRESENT BroadcastReceiver not b

2019-02-27 15:01发布

I see that multiple broadcasts (ACTION_TIME_TICK, for example) cannot be registered in the manifest, the must be explicitly registered via Context.registerReceiver(). I am having trouble with the ACTION_USER_PRESENT broadcast intent. Specifically, I test on the emulator and my application keeps force closing with the error:

08-30 09:44:23.397: ERROR/AndroidRuntime(290): java.lang.RuntimeException: Unable to start receiver me.turnerha.RegisterListeners: java.lang.IllegalArgumentException: Receiver not registered: me.turnerha.RegisterListeners@43d05690

This is caused by

08-30 09:44:23.397: ERROR/AndroidRuntime(290): Caused by: java.lang.IllegalArgumentException: Receiver not registered: me.turnerha.RegisterListeners@43d05690

My manifest is fairly simple:

    <receiver android:name=".RegisterListeners">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_ON" />
        </intent-filter>
    </receiver>

Any thoughts? I am essentially attempting to create a Receiver that is awakened as soon as possible after my application is installed. The first time it is awakened, it registers a few listeners, and then it unregisters itself so it is never called again. (I really wish there was an intent fired immediately after your app had been installed, to allow a small bit of setup :) )

4条回答
叼着烟拽天下
2楼-- · 2019-02-27 15:41

I had the same problem and I fixed it (tested on 4.3 and 5.1). I WAS able to declare "android.intent.action.USER_PRESENT" inside the manifest, as long as you have the READ_PHONE_STATE permission, it is OK!! My mini app consists of a Broadcast receiver that reacts to the screen ON/OFF state, and runs a background service that does continuous voice recognition. If the screen is off, the recognition is turned off. Here is the code, enjoy: MANIFEST:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <receiver android:name="classes.VoiceLaunchReceiver" >
            <intent-filter>                
                <action android:name="android.intent.action.USER_PRESENT" />    
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

BROADCAST RECEIVER:

public class VoiceLaunchReceiver extends BroadcastReceiver {
    @Override  
    public void onReceive(Context ctx, Intent intent) {     
        Intent service = new Intent(ctx, VoiceLaunchService.class);
     //   service.putExtra(action, true);
        Log.i("joscsr","Incoming Voice Launch Broadcast...");  

        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
            Log.i("joshcsr", "************\nCSR Resumed (BC)\n************");
            ctx.startService(service);
            }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i("joshcsr", "************\nCSR STOPPED by SCREEN (BC)\n************");
            ctx.stopService(service);  
            }
        }  
}

As you can imagine, my USER_PRESENT broadcast receiver is not registered anywhere else. I do register ACTION_SCREEN_OFF and ON in the onCreate method of my service, who was triggered by my receiver.

@Override
public void onCreate() {
    super.onCreate();
    //Register screen ON/OFF BroadCast
    launcher=new VoiceLaunchReceiver();
    IntentFilter i=new IntentFilter(Intent.ACTION_SCREEN_OFF);
    i.addAction(Intent.ACTION_SCREEN_ON);               
    registerReceiver(launcher,i);
    Log.d("joshcsr","VoiceLaunch Service CREATED"); 
    }

Finally I unregister the screen on/off in the onDestroy() of my service:

@Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(launcher);}
查看更多
时光不老,我们不散
3楼-- · 2019-02-27 15:45

registering ACTION_USER_PRESENT in manifest file does not get triggered always. In my nexus4 if i register ACTION_USER_PRESENT in manifest file then it does not work at all whereas registering in Activity works fine.

查看更多
爷的心禁止访问
4楼-- · 2019-02-27 16:00

Correct -- neither ACTION_SCREEN_ON nor ACTION_USER_PRESENT can be registered in the manifest. I have filed a documentation bug on this issue.

查看更多
神经病院院长
5楼-- · 2019-02-27 16:05

Eitherway, you could use context.registerReceiver() which will do the trick, and keep your manifest clean. Link

查看更多
登录 后发表回答