萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android開發之電源管理

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;

  

  ImageButt

copyright © 萬盛學電腦網 all rights reserved