充电时自动启动应用程序

时间:2012-06-02 09:44:35

标签: android

我正在创建一个Android应用程序,我想在移动设备插入充电或插入耳机后立即自动启动。请提供任何解决方案。

5 个答案:

答案 0 :(得分:6)

嘿,我准备一个电池充电演示,试试吧。

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

}

答案 1 :(得分:5)

要检测充电状态,请使用BroadcastReceiver并在清单文件中注册,如::

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

并检测耳机是否插入或未使用AudioManager.isWiredHeadsetOn()

答案 2 :(得分:3)

检查出来

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

接收

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

同时接收

<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>
你的清单中的

答案 3 :(得分:2)

对于遇到同样问题的人: Detecting the device being plugged in

I found this other site as well,帮了我一堆!

答案 4 :(得分:2)

像这样:

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

然后:

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