5

我正在创建一个 Android 应用程序,我想在手机插入充电或耳机插入后立即自动启动。请提供有关如何执行此操作的任何解决方案。

4

5 回答 5

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>

电源连接接收器

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

}
于 2012-06-02T11:59:39.363 回答
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()

于 2012-06-02T09:57:27.643 回答
2

看一下这个

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>

在你的清单中

于 2012-06-02T09:50:55.130 回答
2

对于任何有同样问题的人: 检测正在插入的设备

我也找到了这个其他网站,这对我有很大帮助!

于 2014-03-12T14:23:03.203 回答
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
于 2016-05-24T06:44:26.457 回答