接收充电状态和电量的系统广播

来源:互联网 发布:河南房卡麻将源码 编辑:程序博客网 时间:2024/05/18 02:58
public class BroadcastR_PowerListener extends BroadcastReceiver {    private static final String ACTION_POWER_CONNECTED = "android.intent.action.ACTION_POWER_CONNECTED";    private static final String ACTION_POWER_DISCONNECTED = "android.intent.action.ACTION_POWER_DISCONNECTED";    private static final String POWER_LOG_PATH = "Cache";    private static BroadcastReceiver instance = null;    private BroadcastR_PowerListener() {    }    public static void registerPowerListener() {        if (instance == null) {            instance = new BroadcastR_PowerListener();            IntentFilter intentFilter = new IntentFilter();            intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);            intentFilter.addAction(ACTION_POWER_CONNECTED);            intentFilter.addAction(ACTION_POWER_DISCONNECTED);            intentFilter.setPriority(Integer.MAX_VALUE);            MyApplication.getInstance().registerReceiver(instance, intentFilter);        }    }    @Override    public void onReceive(Context context, Intent intent) {        if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {            //你可以读到充电状态,如果在充电,可以读到是usb还是交流电            // 是否在充电            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;            String fileName = String.format("uid_%d_power.txt", User.getUid());            int powerLevel = intent.getIntExtra("level", 0);            if (usbCharge) {                write(powerLevel+"", POWER_LOG_PATH, fileName);            }        } else if (intent.getAction().equals(ACTION_POWER_CONNECTED)) {            ErrRpt.debug("powerlistener", "Power Connected.");        } else if (intent.getAction().equals(ACTION_POWER_DISCONNECTED)) {            ErrRpt.debug("powerlistener", "Power DisConnected.");        }    }    private void write(String content, String directory, String fileName) {        try {            File dir = new File(Environment.getExternalStorageDirectory()                    + "/" + directory);            File file = new File(dir + "/" + fileName);            if (!dir.exists()) {                dir.mkdirs();            }            if (!file.exists()) {                try {                    file.createNewFile();                } catch (IOException e) {                    e.printStackTrace();                }            }            //true: append 追加的方式打开            FileOutputStream fouts = new FileOutputStream(file, false);            //将FileOutputStream包装成PrintStream            PrintStream ps = new PrintStream(fouts);            //输出文件内容            ps.print(content);            ps.close();            fouts.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

0 0
原创粉丝点击