5

I am creating an Android application that I want to auto-start as soon as the mobile is plugged into charging or headphone is plugged into it. Please provide any solution as to how to do it.

Sparky
  • 8,437
  • 1
  • 29
  • 41
Ankit
  • 137
  • 1
  • 1
  • 10

5 Answers5

6

Hey I prepare a demo for battery charging, just try it.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.logistic.test1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.BATTERY_STATS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Test1Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".PowerConnectionReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            </intent-filter>
        </receiver>
        <class android:name=".PowerConnectionReceiver">
        </class>

</application>
</manifest>

PowerConnectionReceiver

package com.logistic.test1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.widget.Toast;

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isCharging = false;
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        if(status == 2)
            isCharging = true;
        //boolean isCharging = status == BatteryManager.BATTERY_PLUGGED_AC ||
            //  status == BatteryManager.BATTERY_PLUGGED_USB;
                //BATTERY_STATUS_CHARGING;
                //  || status == BatteryManager.BATTERY_STATUS_FULL;
        /*
         * int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
         * -1); boolean usbCharge = chargePlug ==
         * BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug ==
         * BatteryManager.BATTERY_PLUGGED_AC;
         */
        Toast.makeText(context, "Status : "+status+"\nCharging : "+isCharging, Toast.LENGTH_SHORT).show();
    }

}

Test1Activity.java

package com.logistic.test1;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class Test1Activity extends Activity {
    TextView tv1, tv2;
    PowerConnectionReceiver pcr, pcr2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onStart() {
        super.onStart();
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = registerReceiver(null, ifilter);
        pcr = new PowerConnectionReceiver();
        pcr.onReceive(getApplicationContext(), batteryStatus);
    }

    @Override
    public void onStop() {
        super.onStop();
        try {
            unregisterReceiver(pcr);
        } catch(IllegalStateException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter);
        pcr2 = new PowerConnectionReceiver();
        pcr2.onReceive(getApplicationContext(), batteryStatus);
    }

    @Override
    public void onPause() {
        super.onPause();
        try {
            unregisterReceiver(pcr2);
        } catch(IllegalStateException e) {
            e.printStackTrace();
        }
    }

    private void showText(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

}
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
5

To detect charging state, use BroadcastReceiver and register in your manifest file like ::

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

and to detect headphone is plugged or not use AudioManager.isWiredHeadsetOn()

Eight
  • 4,194
  • 5
  • 30
  • 51
2

check this out

http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

Receive

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

Also receive

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

in your manifest

Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
2

For anyone having the same problem: Detecting the device being plugged in

and I found this other site as well, which helped me a bunch!

Community
  • 1
  • 1
Aart den Braber
  • 864
  • 1
  • 11
  • 23
2

like this:

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

then:

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);


        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging =    status == BatteryManager.BATTERY_STATUS_CHARGING ||
                                status == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge   = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge    = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;


        if (batteryStatus != null) {
            int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            float batteryPct = level / (float) scale;
        }


    }//end onReceive


}//end PowerConnectionReceiver
Thiago
  • 12,778
  • 14
  • 93
  • 110
  • What if the batterypct is not in the range I want to start my activity? Let's say I want to start an activity only if it's charging AND battery level is above 90%, how to accomplish that? – Dpedrinha May 08 '18 at 11:34