Android电量和插拔电源状态广播监听

Android电量广播状态有3种,电量低,电量充满,电量发生改变。 
Action是:

Intent.ACTION_BATTERY_CHANGE
Intent.ACTION_BATTERY_LOW
Intent.ACTION_BATTERY_OKAY

Android的插拔电源广播的Action是:

Intent.ACTION_POWER_CONNECTED
Intent.ACTION_POWER_DISCONNECTED

示例代码:

public class BatteryListener {

    private Context mContext;

    private BatteryBroadcastReceiver receiver;

    private BatteryStateListener mBatteryStateListener;

    public BatteryListener(Context context) {
        mContext = context;
        receiver = new BatteryBroadcastReceiver();
    }

    public void register(BatteryStateListener listener) {
        mBatteryStateListener = listener;
        if (receiver != null) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_BATTERY_CHANGED);
            filter.addAction(Intent.ACTION_BATTERY_LOW);
            filter.addAction(Intent.ACTION_BATTERY_OKAY);
            filter.addAction(Intent.ACTION_POWER_CONNECTED);
            filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
            mContext.registerReceiver(receiver, filter);
        }
    }

    public void unregister() {
        if (receiver != null) {
            mContext.unregisterReceiver(receiver);
        }
    }

    private class BatteryBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String acyion = intent.getAction();
                switch (acyion) {
                    case Intent.ACTION_BATTERY_CHANGED://电量发生改变
                        if (mBatteryStateListener != null) {
                            Log.e("zhang", "BatteryBroadcastReceiver --> onReceive--> ACTION_BATTERY_CHANGED");
                            mBatteryStateListener.onStateChanged();
                        }
                        break;
                    case Intent.ACTION_BATTERY_LOW://电量低
                        if (mBatteryStateListener != null) {
                            Log.e("zhang", "BatteryBroadcastReceiver --> onReceive--> ACTION_BATTERY_LOW");
                            mBatteryStateListener.onStateLow();
                        }
                        break;
                    case Intent.ACTION_BATTERY_OKAY://电量充满
                        if (mBatteryStateListener != null) {
                            Log.e("zhang", "BatteryBroadcastReceiver --> onReceive--> ACTION_BATTERY_OKAY");
                            mBatteryStateListener.onStateOkay();
                        }
                        break;
                    case Intent.ACTION_POWER_CONNECTED://接通电源
                        if (mBatteryStateListener != null) {
                            Log.e("zhang", "BatteryBroadcastReceiver --> onReceive--> ACTION_POWER_CONNECTED");
                            mBatteryStateListener.onStatePowerConnected();
                        }
                        break;
                    case Intent.ACTION_POWER_DISCONNECTED://拔出电源
                        if (mBatteryStateListener != null) {
                            Log.e("zhang", "BatteryBroadcastReceiver --> onReceive--> ACTION_POWER_DISCONNECTED");
                            mBatteryStateListener.onStatePowerDisconnected();
                        }
                        break;
                }
            }
        }
    }

    public interface BatteryStateListener {
        public void onStateChanged();

        public void onStateLow();

        public void onStateOkay();

        public void onStatePowerConnected();

        public void onStatePowerDisconnected();
    }

}

MainActivity中调用:

public class MainActivity extends AppCompatActivity {

    private BatteryListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listener = new BatteryListener(this);
        listener.register(new BatteryListener.BatteryStateListener() {
            @Override
            public void onStateChanged() {
                Log.e("zhang", "MainActivity --> onStateChanged--> ");
            }

            @Override
            public void onStateLow() {
                Log.e("zhang", "MainActivity --> onStateLow--> ");
                Toast.makeText(MainActivity.this, "onStateLow", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStateOkay() {
                Log.e("zhang", "MainActivity --> onStateOkay--> ");
                Toast.makeText(MainActivity.this, "onStateOkay", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStatePowerConnected() {
                Log.e("zhang", "MainActivity --> onStatePowerConnected--> ");
                Toast.makeText(MainActivity.this, "onStatePowerConnected", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStatePowerDisconnected() {
                Log.e("zhang", "MainActivity --> onStatePowerDisconnected--> ");
                Toast.makeText(MainActivity.this, "onStatePowerDisconnected", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (listener != null) {
            listener.unregister();
        }
        super.onDestroy();
    }
}

文章来源:https://blog.csdn.net/qq_33689414/article/details/53769800

猜你喜欢

转载自blog.csdn.net/augfun/article/details/88600217