Android中獲取電池電量實例代碼
更新時間:2013年06月20日 15:15:19 作者:
顯示當前電池電量在特殊的需求下還是蠻有用的,本文寫了一個可以實現(xiàn)簡單的功能,感興趣的朋友可以參考下哈
復制代碼 代碼如下:
/**
*
* @author chrp
*
*顯示當前電池電量
*/
public class MainActivity extends Activity {
private TextView tv;
/**
* 廣播接受者
*/
class BatteryReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//判斷它是否是為電量變化的Broadcast Action
if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())){
//獲取當前電量
int level = intent.getIntExtra("level", 0);
//電量的總刻度
int scale = intent.getIntExtra("scale", 100);
//把它轉(zhuǎn)成百分比
tv.setText("電池電量為"+((level*100)/scale)+"%");
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
tv = new TextView(this);
tv.setText("chrp");
this.setContentView(tv);
//注冊廣播接受者java代碼
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
//創(chuàng)建廣播接受者對象
BatteryReceiver batteryReceiver = new BatteryReceiver();
//注冊receiver
registerReceiver(batteryReceiver, intentFilter);
}
相關文章
Android Activity 與Service進行數(shù)據(jù)交互詳解
這篇文章主要介紹了Android Activity 與Service進行數(shù)據(jù)交互的相關資料,在開發(fā)Android App的時候經(jīng)常會使用這樣的功能,需要的朋友可以參考下2016-10-10
Android中定時執(zhí)行任務的3種實現(xiàn)方法(推薦)
下面小編就為大家?guī)硪黄狝ndroid中定時執(zhí)行任務的3種實現(xiàn)方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
android支付寶客戶端html5網(wǎng)頁無法自動關閉問題的解決方法
這篇文章主要為大家詳細介紹了android支付寶客戶端html5網(wǎng)頁無法自動關閉問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Android實現(xiàn)志愿者系統(tǒng)詳細步驟與代碼
這篇文章主要介紹了Android實現(xiàn)志愿者系統(tǒng),本系統(tǒng)采用MVC架構(gòu)設計,SQLite數(shù)據(jù)表有用戶表、成員表和活動表,有十多個Activity頁面。打開應用,進入歡迎界面,3s后跳轉(zhuǎn)登錄界面,用戶先注冊賬號,登錄成功后進入主界面2023-02-02
Android中實現(xiàn)長按照片彈出右鍵菜單功能的實例代碼
這篇文章主要介紹了Android中實現(xiàn)長按照片彈出右鍵菜單功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01

