Reading of Battery level on G1

转自 http://www.maximyudin.com/2009/01/05/android/reading-of-battery-level-on-g1/

用1.5sdk编译后试着在自己的G1(已经升级到1。5版本)里跑了一下,果然能得到电池电量。 high

Activity code:

package maximyudin.BatteryLevel;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.widget.TextView;public class BatteryLevel extends Activity {     private TextView tvBatteryLevel;     private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {          @Override          public void onReceive(Context context, Intent intent) {               String action = intent.getAction();               if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {                    int level = intent.getIntExtra("level", 0);                    int scale = intent.getIntExtra("scale", 100);                    tvBatteryLevel.setText("Battery level: "                              + String.valueOf(level * 100 / scale) + "%");               }          }     };     @Override     public void onResume() {          super.onResume();          registerReceiver(mBatteryInfoReceiver, new IntentFilter(                    Intent.ACTION_BATTERY_CHANGED));     }     @Override     public void onPause() {          super.onPause();          unregisterReceiver(mBatteryInfoReceiver);     }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tvBatteryLevel = (TextView) findViewById(R.id.tvBatteryLevel);    }}

Activity layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:id="@+id/tvBatteryLevel" android:text="Battery level:"/></LinearLayout>

更多相关文章

  1. Android(安卓)Studio 2.3版本 Run项目不能自动启动APP的问题
  2. Android(安卓)Studio 出现Error:Connection timed out: connect.
  3. 【翻译】Android(安卓)Support Library Features(二)
  4. Gradle Error: Connection timed out 无法获取远程依赖解决方法(2
  5. Android(安卓)Studio gradle插件版本和gradle版本对应关系
  6. Android高版本不兼容低版本gradle问题
  7. 在Android中使用Gradle
  8. 在eclipse中查看Android各版本源代码
  9. Android(安卓)NDK在Mac OS 上面编译FFmpeg时遇见的坑?

随机推荐

  1. EditText
  2. Android.Hacks.01_Centering views using
  3. android 开发技巧(8)--格式化 TextView
  4. 查看 apk 的信息
  5. android 实现左右拖动的网页焦点图
  6. android 调用相机拍照 并缩放切割图片
  7. Android中通过资源文件获取drawable的几
  8. Android(安卓)Studio BUILD FAILED finis
  9. 2011.09.23——— android sample之Notep
  10. android——xml drawable实现背景样式