基本信息
源码名称:android 手机电量计算 相关文档
源码大小:10.86M
文件格式:.zip
开发语言:Java
更新时间:2014-10-07
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.android.batterywaster; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.os.PowerManager; import android.view.View; import android.widget.CheckBox; import android.widget.TextView; import java.text.DateFormat; import java.util.Date; /** * So you thought sync used up your battery life. */ public class BatteryWaster extends Activity { TextView mLog; DateFormat mDateFormat; IntentFilter mFilter; PowerManager.WakeLock mWakeLock; SpinThread mThread; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the layout for this activity. You can find it // in res/layout/hello_activity.xml setContentView(R.layout.main); findViewById(R.id.checkbox).setOnClickListener(mClickListener); mLog = (TextView)findViewById(R.id.log); mDateFormat = DateFormat.getInstance(); mFilter = new IntentFilter(); mFilter.addAction(Intent.ACTION_BATTERY_CHANGED); mFilter.addAction(Intent.ACTION_BATTERY_LOW); mFilter.addAction(Intent.ACTION_BATTERY_OKAY); mFilter.addAction(Intent.ACTION_POWER_CONNECTED); PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "BatteryWaster"); mWakeLock.setReferenceCounted(false); } @Override public void onPause() { stopRunning(); } View.OnClickListener mClickListener = new View.OnClickListener() { public void onClick(View v) { CheckBox checkbox = (CheckBox)v; if (checkbox.isChecked()) { startRunning(); } else { stopRunning(); } } }; void startRunning() { log("Start"); registerReceiver(mReceiver, mFilter); mWakeLock.acquire(); if (mThread == null) { mThread = new SpinThread(); mThread.start(); } } void stopRunning() { log("Stop"); unregisterReceiver(mReceiver); mWakeLock.release(); if (mThread != null) { mThread.quit(); mThread = null; } } void log(String s) { mLog.setText(mLog.getText() "\n" mDateFormat.format(new Date()) ": " s); } BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); String title = action; int index = title.lastIndexOf('.'); if (index >= 0) { title = title.substring(index 1); } if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int icon = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,-1); log(title ": level=" level "\n" "icon:" icon); } else { log(title); } } }; class SpinThread extends Thread { private boolean mStop; public void quit() { synchronized (this) { mStop = true; } } public void run() { while (true) { synchronized (this) { if (mStop) { return; } } } } } }