Android_(自動化)自動擷取手機電池的剩餘電量

來源:互聯網
上載者:User

標籤:mba   ext   ppc   ttext   ack   tool   evel   cte   button   

 

 自動擷取手機電池的剩餘電量

 

通過使用BroadcastReceiver的特性來擷取手機電池的電量,註冊BroadcastReceiver時設定的IntentFilter來擷取系統發出的Intent.ACTION_BATTERY_CHANGED,然後以此來擷取電池的電量。

 

運行:

 

程式結構

 

package com.example.asus.gary_040a;import android.app.Dialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.TextView;import org.w3c.dom.Text;public class MainActivity extends AppCompatActivity {    private int intLevel;    private int intScale;    private Button mButton01;    private TextView tv;    //建立BroadcastReceiver    private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            String action = intent.getAction();            //如果捕捉到的Action是ACTION_BATTERY_CHANGED則運行onBatteryInforECEIVER()            if(intent.ACTION_BATTERY_CHANGED.equals(action))            {                //獲得當前電量                intLevel = intent.getIntExtra("level",0);                //獲得手機總電量                intScale = intent.getIntExtra("scale",100);                 // 在下面會定義這個函數,顯示手機當前電量                onBatteryInfoReceiver(intLevel, intScale);            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mButton01 = (Button) findViewById(R.id.myButton1);        mButton01.setOnClickListener(new Button.OnClickListener() {            @Override            public void onClick(View v) {                // 註冊一個BroadcastReceiver,作為訪問電池計量之用                registerReceiver(mBatInfoReveiver, new IntentFilter(                        Intent.ACTION_BATTERY_CHANGED));            }        });    }    // 攔截到ACTION_BATTRY_CHANGED後要執行的動作    private void onBatteryInfoReceiver(int intLevel, int intScale) {        // TODO Auto-generated method stub         int percent = intLevel*100/ intScale;        //得到的person就是百分比電量        //不乘100得到的percent為0        tv=(TextView) findViewById(R.id.myTextView02);        tv.setText("現在的電量是"+percent+"%。");    };}
MainActivity

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.asus.gary_040a.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Gary_自動擷取手機電池的剩餘電量!"        android:textSize="40px"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <Button        android:id="@+id/myButton1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" />    <TextView        android:id="@+id/myTextView02"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="50px" /></LinearLayout>
acvivity_main

 

一、介面布局

  一個Button按鈕,一個TextView文字框  

  點擊Button時會在TextView上顯示(手機電量)提示

 

二、實現程式功能

  1、如果捕捉的Action是ACTION_BATTERY_CHANGED則運行onBatteryInfoReceiver()顯示當前手機電量

 private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            String action = intent.getAction();            //如果捕捉到的Action是ACTION_BATTERY_CHANGED則運行onBatteryInforECEIVER()            if(intent.ACTION_BATTERY_CHANGED.equals(action))            {                //獲得當前電量                intLevel = intent.getIntExtra("level",0);                //獲得手機總電量                intScale = intent.getIntExtra("scale",100);                 // 在下面會定義這個函數,顯示手機當前電量                onBatteryInfoReceiver(intLevel, intScale);            }        }    };

  2、對Button按鈕添加單擊後的事件響應動作,註冊系統BroadcastReceiver廣播事件來訪問電池電量

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mButton01 = (Button) findViewById(R.id.myButton1);        mButton01.setOnClickListener(new Button.OnClickListener() {            @Override            public void onClick(View v) {                // 註冊一個BroadcastReceiver,作為訪問電池計量之用                registerReceiver(mBatInfoReveiver, new IntentFilter(                        Intent.ACTION_BATTERY_CHANGED));            }        });    }

  3、定義方法onBatteryInfoReceiver(),通過這個方法能在TextView文字框上顯示手機剩餘電量

 // 攔截到ACTION_BATTRY_CHANGED後要執行的動作    private void onBatteryInfoReceiver(int intLevel, int intScale) {        // TODO Auto-generated method stub         int percent = intLevel*100/ intScale;        //得到的person就是百分比電量        //不乘100得到的percent為0        tv=(TextView) findViewById(R.id.myTextView02);        tv.setText("現在的電量是"+percent+"%。");    };

 

Android_(自動化)自動擷取手機電池的剩餘電量

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.