android 擷取系統電量

來源:互聯網
上載者:User
package irdc.dian;/* import相關class */import irdc.dian.R;import android.app.Activity;import android.app.Dialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.TextView;public class dian extends Activity{  /* 變數聲明 */  private int intLevel;  private int intScale;  private Button mButton01;  /* 建立BroadcastReceiver */  private BroadcastReceiver mBatInfoReceiver=new BroadcastReceiver()  {    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);      }    }  };  /** Called when the activity is first created. */  @Override   public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    /* 載入main.xml Layout */    setContentView(R.layout.main);    /* 初始化Button,並設定點擊後的動作 */    mButton01 = (Button)findViewById(R.id.myButton1);    mButton01.setOnClickListener(new Button.OnClickListener()    {      @Override      public void onClick(View v)      {        /* 註冊一個系統 BroadcastReceiver,作為訪問電池計量之用 */        registerReceiver        (          mBatInfoReceiver,          new IntentFilter(Intent.ACTION_BATTERY_CHANGED)        );      }    });  }  /* 捕捉到ACTION_BATTERY_CHANGED時要啟動並執行method */  public void onBatteryInfoReceiver(int intLevel, int intScale)  {    /* create 跳出的交談視窗 */    final Dialog d = new Dialog(dian.this);    d.setTitle(R.string.str_dialog_title);    d.setContentView(R.layout.mydialog);    /* 建立一個背景模糊的Window,且將交談視窗放在前景 */    Window window = d.getWindow();    window.setFlags    (      WindowManager.LayoutParams.FLAG_BLUR_BEHIND,      WindowManager.LayoutParams.FLAG_BLUR_BEHIND    );    /* 將取得的電池計量顯示於Dialog中 */    TextView mTextView02=(TextView)d.findViewById(R.id.myTextView2);    mTextView02.setText     (      getResources().getText(R.string.str_dialog_body)+       String.valueOf(intLevel * 100 / intScale) + "%"     );    /* 設定返回主畫面的按鈕 */    Button mButton02 = (Button)d.findViewById(R.id.myButton2);     mButton02.setOnClickListener(new Button.OnClickListener()    {      @Override      public void onClick(View v)      {        /* 反註冊Receiver,並關閉交談視窗 */        unregisterReceiver(mBatInfoReceiver);        d.dismiss();       }    });    d.show();   }}

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/layout1"  android:background="@drawable/white"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <TextView     android:id="@+id/myTextView1"     android:layout_width="fill_parent"      android:layout_height="wrap_content"     android:textColor="@drawable/blue"     android:textSize="20sp"     android:text="@string/str_title"    android:layout_x="60px"    android:layout_y="40px"  >  </TextView>  <Button    android:id="@+id/myButton1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/str_button1"    android:textColor="@drawable/black"     android:textSize="14sp"     android:layout_x="80px"    android:layout_y="90px"  >  </Button> </AbsoluteLayout>

<?xml version="1.0" encoding="utf-8"?> <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:background="@drawable/white"   android:orientation="vertical"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   >   <TextView     android:id="@+id/myTextView2"     android:layout_width="fill_parent"      android:layout_height="wrap_content"     android:textColor="@drawable/blue"    android:textSize="16sp"    android:gravity="center"    android:padding="10px"  >  </TextView>   <Button     android:id="@+id/myButton2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/str_button2"  >  </Button> </LinearLayout>

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="irdc.dian"      android:versionCode="1"      android:versionName="1.0.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name="irdc.dian.dian"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest> 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.