Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> [Android開發學習32]BroadcastReceiver之電源管理

[Android開發學習32]BroadcastReceiver之電源管理

編輯:關於Android編程

直接上代碼:

[java]
/* ========================== Battery Start =============================*/ 
     
    int tBatteryLevel = 0, tBatteryCurLevel = 0; 
    int tBatteryChargeFlag = -1; 
     
    private void Battery(){ 
        int tTempFlag = 0; 
         
        ImageButton tTempButton = (ImageButton)findViewById(R.id.top_battery); 
        tTempButton.setVisibility(View.INVISIBLE); 
         
        IntentFilter   mIntentFilter = new IntentFilter();   
        mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);     
         
         
        // 聲明消息處理過程  
        BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 
 
            @Override 
            public void onReceive(Context context, Intent intent) { 
                // TODO Auto-generated method stub  
                String action = intent.getAction(); 
                if(action.equals(Intent.ACTION_BATTERY_CHANGED)){ 
                    //電池電量,數字   
                    int tTempLevel = intent.getIntExtra("level", 0); 
                    //電池最大容量  
                    int tTempScale = intent.getIntExtra("scale", 100); 
                    //電池狀態,返回是一個數字       
                    // BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態       
                    // BatteryManager.BATTERY_STATUS_DISCHARGING 放電中       
                    // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電       
                    // BatteryManager.BATTERY_STATUS_FULL 電池滿  
                    int tTempStatus = intent.getIntExtra("status", 0); 
                     
                    tTempLevel = (tTempLevel * 100) / tTempScale; 
                    if (tTempLevel > 95) 
                        tBatteryLevel = 5; 
                    else if(tTempLevel > 80) 
                        tBatteryLevel = 4; 
                    else if(tTempLevel > 60) 
                        tBatteryLevel = 3; 
                    else if(tTempLevel > 40) 
                        tBatteryLevel = 2; 
                    else if(tTempLevel > 20) 
                        tBatteryLevel = 1; 
                    else 
                        tBatteryLevel = 0; 
                     
                    if (tTempStatus == BatteryManager.BATTERY_STATUS_CHARGING) 
                        tBatteryChargeFlag = 1; 
                    else 
                        tBatteryChargeFlag = 0; 
                } 
            } 
             
        }; 
 
        registerReceiver(mIntentReceiver, mIntentFilter);  
    } 
/* ========================== Battery End   =============================*/ 

/* ========================== Battery Start =============================*/
 
 int tBatteryLevel = 0, tBatteryCurLevel = 0;
 int tBatteryChargeFlag = -1;
 
 private void Battery(){
  int tTempFlag = 0;
  
  ImageButton tTempButton = (ImageButton)findViewById(R.id.top_battery);
  tTempButton.setVisibility(View.INVISIBLE);
  
  IntentFilter   mIntentFilter = new IntentFilter(); 
  mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);   
  
  
  // 聲明消息處理過程
  BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {

   @Override
   public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String action = intent.getAction();
    if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
     //電池電量,數字
     int tTempLevel = intent.getIntExtra("level", 0);
     //電池最大容量
     int tTempScale = intent.getIntExtra("scale", 100);
     //電池狀態,返回是一個數字    
     // BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態    
     // BatteryManager.BATTERY_STATUS_DISCHARGING 放電中    
     // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電    
     // BatteryManager.BATTERY_STATUS_FULL 電池滿
     int tTempStatus = intent.getIntExtra("status", 0);
     
     tTempLevel = (tTempLevel * 100) / tTempScale;
     if (tTempLevel > 95)
      tBatteryLevel = 5;
     else if(tTempLevel > 80)
      tBatteryLevel = 4;
     else if(tTempLevel > 60)
      tBatteryLevel = 3;
     else if(tTempLevel > 40)
      tBatteryLevel = 2;
     else if(tTempLevel > 20)
      tBatteryLevel = 1;
     else
      tBatteryLevel = 0;
     
     if (tTempStatus == BatteryManager.BATTERY_STATUS_CHARGING)
      tBatteryChargeFlag = 1;
     else
      tBatteryChargeFlag = 0;
    }
   }
   
  };

  registerReceiver(mIntentReceiver, mIntentFilter);
 }
/* ========================== Battery End   =============================*/
 

 

另外還有一份別人的參考代碼:

[java]
檢測到現在在電源狀態: 
 
IntentFilter   mIntentFilter = new IntentFilter(); 
 
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);   
registerReceiver(mIntentReceiver, mIntentFilter); 
 
 //聲明消息處理過程    
   private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {   
       @Override   
       public void onReceive(Context context, Intent intent) {   
           String action = intent.getAction();   
           //要看看是不是我們要處理的消息    
           if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {              
               //電池電量,數字    
               Log.d("Battery", "" + intent.getIntExtra("level", 0));                  
               //電池最大容量    
               Log.d("Battery", "" + intent.getIntExtra("scale", 0));                  
               //電池伏數    
               Log.d("Battery", "" + intent.getIntExtra("voltage", 0));                  
               //電池溫度    
               Log.d("Battery", "" + intent.getIntExtra("temperature", 0));   
                  
               //電池狀態,返回是一個數字    
               // BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態    
               // BatteryManager.BATTERY_STATUS_DISCHARGING 放電中    
               // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電    
               // BatteryManager.BATTERY_STATUS_FULL 電池滿    
               Log.d("Battery", "" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));   
                 
               //充電類型 BatteryManager.BATTERY_PLUGGED_AC 表示是充電器,不是這個值,表示是 USB    
               Log.d("Battery", "" + intent.getIntExtra("plugged", 0));   
                 
               //電池健康情況,返回也是一個數字    
               //BatteryManager.BATTERY_HEALTH_GOOD 良好    
               //BatteryManager.BATTERY_HEALTH_OVERHEAT 過熱    
               //BatteryManager.BATTERY_HEALTH_DEAD 沒電    
               //BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 過電壓    
               //BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知錯誤    
               Log.d("Battery", "" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN));   
           }   
       }   
   }; 

檢測到現在在電源狀態:

IntentFilter   mIntentFilter = new IntentFilter();

mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 
registerReceiver(mIntentReceiver, mIntentFilter);

 //聲明消息處理過程 
   private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 
       @Override 
       public void onReceive(Context context, Intent intent) { 
           String action = intent.getAction(); 
           //要看看是不是我們要處理的消息 
           if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {            
               //電池電量,數字 
               Log.d("Battery", "" + intent.getIntExtra("level", 0));                
               //電池最大容量 
               Log.d("Battery", "" + intent.getIntExtra("scale", 0));                
               //電池伏數 
               Log.d("Battery", "" + intent.getIntExtra("voltage", 0));                
               //電池溫度 
               Log.d("Battery", "" + intent.getIntExtra("temperature", 0)); 
                
               //電池狀態,返回是一個數字 
               // BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態 
               // BatteryManager.BATTERY_STATUS_DISCHARGING 放電中 
               // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電 
               // BatteryManager.BATTERY_STATUS_FULL 電池滿 
               Log.d("Battery", "" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN)); 
               
               //充電類型 BatteryManager.BATTERY_PLUGGED_AC 表示是充電器,不是這個值,表示是 USB 
               Log.d("Battery", "" + intent.getIntExtra("plugged", 0)); 
               
               //電池健康情況,返回也是一個數字 
               //BatteryManager.BATTERY_HEALTH_GOOD 良好 
               //BatteryManager.BATTERY_HEALTH_OVERHEAT 過熱 
               //BatteryManager.BATTERY_HEALTH_DEAD 沒電 
               //BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 過電壓 
               //BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知錯誤 
               Log.d("Battery", "" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)); 
           } 
       } 
   };
 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved