Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android--電池相關信息的獲取

Android--電池相關信息的獲取

編輯:關於Android編程

1.定義廣播接收,顯示電池信息--BatteryInfoBroadcastReceiver

 

package org.lxh.demo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

public class BatteryInfoBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
			int level = intent.getIntExtra(level, 0);
			int scale = intent.getIntExtra(scale, 0);
			int voltage = intent.getIntExtra(voltage, 0);
			int temperature = intent.getIntExtra(temperature, 0);
			String technology = intent.getStringExtra(technology);
			Dialog dialog = new AlertDialog.Builder(context)
					.setTitle(電池電量)
					.setMessage(
							電池電量為: + String.valueOf(level * 100 / scale)
									+ %
 + 電池電壓為:
									+ String.valueOf((float)voltage / 1000) + v
									+ 
電池類型為: + technology + 
 + 電池溫度為:
									+ String.valueOf((float)temperature / 10) + °C)
					.setNegativeButton(關閉,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface arg0,
										int arg1) {

								}
							}).create();
			dialog.show();
		}

	}

}

2.定義布局管理器--main.xml

 

 



3.定義Activity程序:

 

 

package org.lxh.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Hello extends Activity {
	private Button mybtn = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 設置要使用的布局管理器
		this.mybtn = (Button) super.findViewById(R.id.mybtn);
		this.mybtn.setOnClickListener(new OnClickListenerImpl());

	}

	private class OnClickListenerImpl implements OnClickListener {

		public void onClick(View v) {
			BatteryInfoBroadcastReceiver receiver = null;
			receiver = new BatteryInfoBroadcastReceiver();
			IntentFilter filter = new IntentFilter(
					Intent.ACTION_BATTERY_CHANGED);
			Hello.this.registerReceiver(receiver, filter);

		}

	}
}

在實際手機上運行效果如下:

 

\

 

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