44

I am trying to autostart my nightclock application on charging using the following BroadcastReceiver implemented in the onPause() method:

BroadcastReceiver test = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        unregisterReceiver(this);
        Intent i = new Intent(context, NightClock.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);   
    }           
};
registerReceiver(test, new IntentFilter(Intent.ACTION_POWER_CONNECTED));

The onReceive() method is fired when the USB-cable is plugged in, but the activity doesn't start. However the log shows this:

I/ActivityManager(   79): Starting activity: Intent { flg=0x10000000 cmp=com.meins.nightclock/.NightClock }

Any ideas why the log says the activity is started, but nothing happens?

kablu
  • 1
  • 1
  • 6
  • 25
Gubbel
  • 2,296
  • 1
  • 24
  • 35
  • Please update your issue with more from LogCat -- a few lines before this one and a dozen or so lines after this one. Also, why `FLAG_ACTIVITY_NEW_TASK`? Also also, is the activity that registered this `BroadcastReceiver` still around when ACTION_POWER_CONNECTED occurs? – CommonsWare Oct 03 '10 at 13:50
  • There are no more lines in LogCat when connecting to power. The `BroadcastReceiver` is registered in the same activity which it should start. This activity is still running in the background (the LogCat app has been brought to front). If this activity is killed in the taskmanager, the `BroadcastReceiver` doesn't seem to trigger at all. Is this approach to autostart my app wrong from the beginning? – Gubbel Oct 03 '10 at 14:47

3 Answers3

70

You have context passed as parameter to onRecieve() method, so just use:

 @Override
public void onReceive(Context context, Intent intent) {
    //start activity
    Intent i = new Intent();
    i.setClassName("com.test", "com.test.MainActivity");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}

It works, of course you have to change package and activity class name to your own.

Sathish
  • 2,173
  • 2
  • 17
  • 22
  • 2
    Why the downvote? Is this bad? It helped me out so +1 as I could find nothing saying it's a bad approach. – Mafro34 Nov 20 '13 at 15:13
  • @Mafro34 I just tried the same approach and the reason I think it is not ideal is the flag he's setting. Because unless this is what you want (running a new task) it might seriously mess with your application logic - it did with mine anyway. So unless this is what you want I recommend finding another approach. – AgentKnopf Jul 21 '14 at 14:29
  • 7
    @AgentKnopf It looks like you don't have much choice: (from http://developer.android.com/reference/android/content/Context.html#startActivity%28android.content.Intent,%20android.os.Bundle%29 ): "Note that if this method is being called from outside of an Activity Context, then the Intent must include the FLAG_ACTIVITY_NEW_TASK launch flag. This is because, without being started from an existing Activity, there is no existing task in which to place the new activity and thus it needs to be placed in its own separate task." – aberaud Jan 13 '16 at 23:22
  • 1
    FLAG_ACTIVITY_NEW_TASK working well when app is not in forgound/inactive states. I've noticed In MI devices there is one extra setting "Display pop-up windows while running in the background" this is off by default when I on that setting it working well. – Bhavin Chauhan May 13 '20 at 04:06
23

If your goal is that you want NightClock to be started whenever an ACTION_POWER_CONNECTED broadcast is sent, your approach of using a BroadcastReceiver is fine. However, do not register it from an activity. Rather, register it in the manifest:

<receiver android:name=".OnPowerReceiver">
        <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        </intent-filter>
</receiver>

Then, have your BroadcastReceiver as a public Java class (here named OnPowerReceiver, though you can call it whatever you want), and have it call startActivity().

Bear in mind that users probably do not want you doing this. There are many other cases for connecting a phone to power besides starting a "night clock". I humbly suggest you simply let users start your activity via the home screen.

alfoks
  • 3,975
  • 4
  • 27
  • 42
CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367
  • I've registered the `BroadcastReceiver` in the manifest like you said with this call in the `onReceive()` method: `context.startActivity(new Intent(context, NightClock.class));` But nothing happens when I connect the phone to power. Not even a log entry is added if I add `Log.d(this.toString(), "trying to start app ...");` to the method. – Gubbel Oct 03 '10 at 16:02
  • 2
    @Gubbel: Oops. Try ``. Most of the time, they do not have the `ACTION_` in the string, but apparently they do on this one. – CommonsWare Oct 03 '10 at 16:31
  • what will do, if application is removed from task list? Here message will not fire on your broadcast receiver. it will handle by "click_action" parameter. – Md. Sajedul Karim Nov 09 '16 at 13:00
  • you need to set flag `setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)` – Hasan A Yousef Feb 23 '17 at 20:47
-4

From Docs:

Do not start activities from broadcast receivers because the user experience is jarring; especially if there is more than one receiver. Instead, consider displaying a notification.

Ivan
  • 1,173
  • 14
  • 25