8

I want to do something after the the phone is put into charger. So I created ChargingOnReciever:

public class ChargingOnReceiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent intent) { 
        context.startActivity(someActivity);
        Log.d(TAG, "Phone was connected to power");
    } 
} 

and I want my receiver to listen to android.intent.action.ACTION_POWER_CONNECTED, so I put this into manifest:

<reciever android:name=".ChargingOnReceiver"
          android:enabled="true"
          android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
    </intent-filter>
</reciever>

But ChargingOnReceiver is apparently not started when I put my G1 to charger (connect to my notebook via USB cable). Any help is much appreciated.

NatNgs
  • 874
  • 14
  • 25
fhucho
  • 34,062
  • 40
  • 136
  • 186

4 Answers4

29

It's receiver, not reciever! It took me 5 hours to find this stupid bug. I think that the Android Eclipse plugin should do some syntax checking in the manifest xml.

fhucho
  • 34,062
  • 40
  • 136
  • 186
10

For anyone trying to register receiver for "android.intent.action.ACTION_POWER_CONNECTED" and "android.intent.action.ACTION_POWER_DISCONNECTED" , I would like to add :

As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. However, several broadcasts are currently exempted from these limitations. Apps can continue to register listeners for the exempted broadcasts, no matter what API level the apps target.

The above two broadcasts are no longer in the list of these exempted broadcasts. Please refer the documentation below :

https://developer.android.com/guide/components/broadcast-exceptions

Abhishek Luthra
  • 2,575
  • 1
  • 18
  • 34
0
  1. Do not start an activity from a BroadcastReceiver.

  2. Have you examined LogCat at the time you plug in the USB cable to see if there are any logged messages that might explain your problem?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1. I am starting activity only for testing purposes, to see if onReceive is called. 2. Unfortunately I could't find anything helpful in the log messages... – fhucho Jan 20 '10 at 21:57
  • @CommonsWare can u pls explain why should not we start activity from broadcast reciever – user3233280 Jan 10 '15 at 18:01
  • @user3233280: Rarely, if ever, will that be what the user wants, if you are popping up an activity not based on user input. The user may get very irritated if you interrupt their game, and they may get very dead if you interrupt their navigation app. If nothing else, use a full-screen `Notification`. – CommonsWare Jan 10 '15 at 18:10
  • @CommonsWare can u pls tell me i m going to show toast but i have not register reciver from activity while i have registered in my manfiest i m starting activity/showing toast from onRecive() using context but my app gets closed i think context is null thats why it crashed how can i got context if i register reciver in manifest ? – user3233280 Jan 10 '15 at 18:18
0

We cannot receive ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED events on post android 8 as the answer , but still ACTION_BATTERY_CHANGED

private var chargingStateChangeReceiver: BroadcastReceiver? = null
private fun listenForChargeEvents() {
        chargingStateChangeReceiver = object : BroadcastReceiver() {
            override fun onReceive(contxt: Context?, intent: Intent?) {
                val status: Int =
            batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
        if (status == BatteryManager.BATTERY_STATUS_CHARGING
                || status == BatteryManager.BATTERY_STATUS_FULL)
              print('charging')
            }
        }
        context.registerReceiver(chargingStateChangeReceiver, IntentFilter(
            Intent.ACTION_BATTERY_CHANGED)
        )
    }

    override fun onDestroy() {
        super.onCleared()
        context.unregisterReceiver(chargingStateChangeReceiver)
        chargingStateChangeReceiver = null
    }
thanhbinh84
  • 17,876
  • 6
  • 62
  • 69