android.content.Intent#ACTION_BATTERY_OKAY源码实例Demo

下面列出了android.content.Intent#ACTION_BATTERY_OKAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Telephoto   文件: BatteryReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
    String message = null;
    switch (intent.getAction()) {
        case Intent.ACTION_BATTERY_LOW:
            if (!isSentBatteryLow) {
                message = botService.getString(R.string.battery_low);
                isSentBatteryLow = true;
            }
            break;
        case Intent.ACTION_BATTERY_OKAY:
            isSentBatteryLow = false;
            break;
        case Intent.ACTION_POWER_DISCONNECTED:
            message = botService.getString(R.string.power_disconnected) + getStatus();
            break;
        case Intent.ACTION_POWER_CONNECTED:
            message = botService.getString(R.string.power_connected);
            break;
        case Intent.ACTION_BATTERY_CHANGED:
            mBatteryLevel = getBatteryLevel(intent);
            mPowerStatus = getBatteryStatus(intent);
            ReceiverStorage.getInstance().setBatteryLevel(mBatteryLevel);
            int segment = getSegment(mBatteryLevel);
            if (segment < lastSegmentNotify) {
                String attention = mIsCharging ? botService.getString(R.string.battery_discharging) : "";
                message = attention + getStatus();
            }
            lastSegmentNotify = segment;
            break;
    }
    if (message != null) {
        botService.getTelegramService().sendMessageToAll(message);
    }
}
 
源代码2 项目: DevUtils   文件: BatteryReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
    try {
        String action = intent.getAction();
        // 打印当前触发的广播
        LogPrintUtils.dTag(TAG, "onReceive Action: " + action);
        // 获取当前电量, 范围是 0-100
        int level = intent.getIntExtra("level", 0);
        // 判断类型
        switch (action) {
            case Intent.ACTION_BATTERY_CHANGED: // 电量状态发送改变
                if (sListener != null) {
                    sListener.onBatteryChanged(level);
                }
                break;
            case Intent.ACTION_BATTERY_LOW: // 电量低
                if (sListener != null) {
                    sListener.onBatteryLow(level);
                }
                break;
            case Intent.ACTION_BATTERY_OKAY: // 电量从低变回高通知
                if (sListener != null) {
                    sListener.onBatteryOkay(level);
                }
                break;
            case Intent.ACTION_POWER_CONNECTED: // 连接充电器
                if (sListener != null) {
                    sListener.onPowerConnected(level, true);
                }
                break;
            case Intent.ACTION_POWER_DISCONNECTED: // 断开充电器
                if (sListener != null) {
                    sListener.onPowerConnected(level, false);
                }
                break;
            case Intent.ACTION_POWER_USAGE_SUMMARY: // 电力使用情况总结
                if (sListener != null) {
                    sListener.onPowerUsageSummary(level);
                }
                break;
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "onReceive");
    }
}
 
@Override
public void onReceive(Context context, Intent intent) {
    String event = null;
    String type = null;

    switch(intent.getAction()){
        case Intent.ACTION_SCREEN_OFF:
            event = DeviceEvent.EVENT_SCREEN_OFF;
            type = DeviceEvent.TYPE_SCREEN;
            break;

        case Intent.ACTION_SCREEN_ON:
            event = DeviceEvent.EVENT_SCREEN_ON;
            type = DeviceEvent.TYPE_SCREEN;
            break;

        case Intent.ACTION_USER_PRESENT:
            event = DeviceEvent.EVENT_SCREEN_USER_PRESENT;
            type = DeviceEvent.TYPE_SCREEN;
            break;

        case Intent.ACTION_BOOT_COMPLETED:
            event = DeviceEvent.EVENT_BOOT_COMPLETED;
            type = DeviceEvent.TYPE_BOOT;
            break;

        case Intent.ACTION_SHUTDOWN:
            event = DeviceEvent.EVENT_BOOT_SHUTDOWN;
            type = DeviceEvent.TYPE_BOOT;
            break;

        case Intent.ACTION_BATTERY_LOW:
            event = DeviceEvent.EVENT_BATTERY_LOW;
            type = DeviceEvent.TYPE_BATTERY;
            break;

        case Intent.ACTION_BATTERY_OKAY:
            event = DeviceEvent.EVENT_BATTERY_OKAY;
            type = DeviceEvent.TYPE_BATTERY;
            break;

        case Intent.ACTION_POWER_CONNECTED:
            event = DeviceEvent.EVENT_BATTERY_AC_CONNECTED;
            type = DeviceEvent.TYPE_BATTERY;
            break;

        case Intent.ACTION_POWER_DISCONNECTED:
            event = DeviceEvent.EVENT_BATTERY_AC_DISCONNECTED;
            type = DeviceEvent.TYPE_BATTERY;
            break;

        case AudioManager.RINGER_MODE_CHANGED_ACTION:
            AudioManager am = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
            switch (am.getRingerMode()) {
                case AudioManager.RINGER_MODE_SILENT:
                    event = DeviceEvent.EVENT_RINGER_SILENT;
                    type = DeviceEvent.TYPE_RINGER;
                    break;

                case AudioManager.RINGER_MODE_VIBRATE:
                    event = DeviceEvent.EVENT_RINGER_VIBRATE;
                    type = DeviceEvent.TYPE_RINGER;
                    break;

                case AudioManager.RINGER_MODE_NORMAL:
                    event = DeviceEvent.EVENT_RINGER_NORMAL;
                    type = DeviceEvent.TYPE_RINGER;
                    break;
            }
        default:
            break;
    }

    if (type != null)
        output(new DeviceEvent(type, event));
}
 
 方法所在类
 同类方法