获取无线充电信息

时间:2013-08-16 11:34:33

标签: android

我正在编写一个记录电池状态的应用,但BatteryManager.BATTERY_PLUGGED_WIRELESS存在问题。 如果我在无线充电站上加载带有Android 4.0.4甚至S4和4.2.2的Galaxy S3,应用程序将显示1(BATTERY_PLUGGED_AC)而不是4(BATTERY_PLUGGED_WIRELESS)。 电话显示弹出窗口,通知无线充电。这是一个api bug吗?

任何人都可以证实吗?是否有其他解决方法来获得充电模式?

public class BatteryReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
        Toast.makeText(context, errorMsg, Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:-1)

如果我说得对,你想知道设备是否充电。我认为问题解决方案应该这样写:

public class DataReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
    if(intent.getAction().equalsIgnoreCase(Intent.ACTION_POWER_CONNECTED))
    {
        //do something when device is charging
    }
    else if(intent.getAction().equalsIgnoreCase(Intent.ACTION_POWER_DISCONNECTED))
    {
        //do something when device is not charging
    }
}

}

在xml中,接收器将如下所示。

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

希望它对你有所帮助。 : - )