Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto start an app when charging

Tags:

android

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.

like image 951
Ankit Avatar asked Jun 02 '12 09:06

Ankit


2 Answers

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();
    }

}
like image 90
Chintan Raghwani Avatar answered Sep 28 '22 07:09

Chintan Raghwani


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

like image 40
Eight Avatar answered Sep 28 '22 09:09

Eight