• 
    

      <address id="upfr9"><pre id="upfr9"><strike id="upfr9"></strike></pre></address>
      1. <address id="upfr9"><tr id="upfr9"></tr></address><dl id="upfr9"></dl>

        Android電池電量監(jiān)聽(tīng)的示例代碼-創(chuàng)新互聯(lián)

        監(jiān)聽(tīng)電池狀態(tài)只需要接收Intent.ACTION_BATTERY_CHANGED的廣播即可,當(dāng)電池狀態(tài)發(fā)生變化時(shí)會(huì)發(fā)出廣播。

        創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比臺(tái)江網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式臺(tái)江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋臺(tái)江地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

        1.運(yùn)行狀態(tài)如下圖:

        1.充電中的狀態(tài)

        2.未充電時(shí)的狀態(tài)

        2.實(shí)現(xiàn)代碼如下,各個(gè)狀態(tài)通過(guò)名字就很容易知道意思,BatteryManager類中定義了電池狀態(tài)。

        public class MainActivity extends Activity { 
          private static final String TAG = "MainActivity"; 
           
          private TextView mTvVoltage; 
          private TextView mTvTemperature; 
          private TextView mTvLevel; 
          private TextView mTvStatus; 
          private TextView mTvHealth; 
          private TextView mTvTechnology; 
         
          @Override 
          protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.activity_main); 
             
            mTvVoltage = (TextView)findViewById(R.id.tv_voltage); 
            mTvTemperature = (TextView)findViewById(R.id.tv_temperature); 
            mTvLevel = (TextView)findViewById(R.id.tv_level); 
            mTvStatus = (TextView)findViewById(R.id.tv_status); 
            mTvHealth = (TextView)findViewById(R.id.tv_health); 
            mTvTechnology = (TextView)findViewById(R.id.tv_technology); 
             
            this.registerReceiver(this.mBatteryReceiver, new IntentFilter(  
                Intent.ACTION_BATTERY_CHANGED)); 
          } 
         
          @Override 
          public boolean onCreateOptionsMenu(Menu menu) { 
            // Inflate the menu; this adds items to the action bar if it is present. 
            getMenuInflater().inflate(R.menu.main, menu); 
            return true; 
          } 
         
          private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {  
            @Override 
            public void onReceive(Context arg0, Intent arg1) {  
              int voltage=arg1.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0); 
              mTvVoltage.setText("電壓:" + voltage / 1000 + "." + voltage % 1000 + "V"); 
                
              int temperature=arg1.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0); 
              mTvTemperature.setText("溫度:" + temperature / 10 + "." + temperature % 10 + "℃"); 
              if (temperature >= 300) { 
                mTvTemperature.setTextColor(Color.RED); 
              } else { 
                mTvTemperature.setTextColor(Color.BLUE); 
              } 
               
              int level=arg1.getIntExtra(BatteryManager.EXTRA_LEVEL,0); 
              int scale=arg1.getIntExtra(BatteryManager.EXTRA_SCALE,0); 
              int levelPercent = (int)(((float)level / scale) * 100); 
              mTvLevel.setText("電量:" + levelPercent + "%"); 
              if (level <= 10) { 
                mTvLevel.setTextColor(Color.RED); 
              } else { 
                mTvLevel.setTextColor(Color.BLUE); 
              } 
                
              int status = arg1.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN); 
              String strStatus = "未知狀態(tài)";; 
              switch (status) { 
              case BatteryManager.BATTERY_STATUS_CHARGING: 
                strStatus = "充電中……"; 
                break; 
              case BatteryManager.BATTERY_STATUS_DISCHARGING: 
                strStatus = "放電中……"; 
                break; 
              case BatteryManager.BATTERY_STATUS_NOT_CHARGING: 
                strStatus = "未充電"; 
                break; 
              case BatteryManager.BATTERY_STATUS_FULL: 
                strStatus = "充電完成"; 
                break; 
              } 
              mTvStatus.setText("狀態(tài):" + strStatus); 
                
              int health = arg1.getIntExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN); 
              String strHealth = "未知 :(";; 
              switch (status) { 
              case BatteryManager.BATTERY_HEALTH_GOOD: 
                strHealth = "好 :)"; 
                break; 
              case BatteryManager.BATTERY_HEALTH_OVERHEAT: 
                strHealth = "過(guò)熱!"; 
                break; 
              case BatteryManager.BATTERY_HEALTH_DEAD: // 未充電時(shí)就會(huì)顯示此狀態(tài),這是什么鬼? 
                strHealth = "良好"; 
                break; 
              case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: 
                strHealth = "電壓過(guò)高!"; 
                break; 
              case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: 
                strHealth = "未知 :("; 
                break; 
              case BatteryManager.BATTERY_HEALTH_COLD: 
                strHealth = "過(guò)冷!"; 
                break; 
              } 
              mTvHealth.setText("健康狀況:" + strHealth); 
               
              String technology = arg1.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); 
              mTvTechnology.setText("電池技術(shù):" + technology); 
            } 
          }; 
        }
        

        本文標(biāo)題:Android電池電量監(jiān)聽(tīng)的示例代碼-創(chuàng)新互聯(lián)
        本文來(lái)源:http://www.jbt999.com/article18/psjgp.html

        成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、服務(wù)器托管、做網(wǎng)站、定制開(kāi)發(fā)、品牌網(wǎng)站制作搜索引擎優(yōu)化

        廣告

        聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

        外貿(mào)網(wǎng)站建設(shè)

      2. 
        

          <address id="upfr9"><pre id="upfr9"><strike id="upfr9"></strike></pre></address>
          1. <address id="upfr9"><tr id="upfr9"></tr></address><dl id="upfr9"></dl>
            日本最大黄色视频网站 | 婷婷五月天综合 | 天天天日天天天天天天天日歌词 | 日韩午夜福利无码一区不卡 | 国产精品久久久久久久毛片明星 | 亚洲一区欧美日韩国产 云播 | 在线免费日本中文亚洲 | 天天日夜夜艹 | 日本内射在线观看 | 亚洲v日韩V综合V精品V |