2

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

Janusz
  • 187,060
  • 113
  • 301
  • 369
Hamy
  • 20,662
  • 15
  • 74
  • 102

4 Answers4

7

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    I don't know if this was always the case, but `ACTION_USER_PRESENT` *can* be registered in the manifest. It's working just now for me in both the emulator and on my Desire Z (2.2). – Christopher Orr Jul 26 '11 at 08:49
  • 7
    ACTION_USER_PRESENT can be registered in the manifest and works as (seemingly) as long as there is any manner of lock on the screen, including the "drag this bar to unlock" defaults. If you can reach the home screen with no impediments by pressing the power button, the intent does not appear to fire given my testing. – MattC Feb 08 '12 at 18:39
  • @dennisdrew: If you read the collection of answers here, you will find mixed results with respect to `ACTION_USER_PRESENT`. I would not rely upon using that in the manifest. `ACTION_SCREEN_ON` and `ACTION_SCREEN_OFF`, AFAIK, still do not work with manifest-registered receivers. – CommonsWare Sep 24 '15 at 17:34
2

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

Guy
  • 5,370
  • 6
  • 25
  • 30
  • 2
    As far as I know, these kind of receiver will only life as long as your application. And especially on Samsung this time can be very short. – JacksOnF1re May 24 '16 at 13:17
1

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.

Ashu Singh
  • 341
  • 4
  • 7
0

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);}
Josh
  • 6,251
  • 2
  • 46
  • 73