效果图:

android之检测手机电池_第1张图片

activity代码:

package cn.com.chenzheng_java; 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.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /*** * @description 查看手机电池电量,这里要注意监听Intent.ACTION_BATTERY_CHANGED的广播接收者,只能 * 在程序中显示的注册,不能再androiManifest.xml中进行静态的注册 * @author chenzheng_java * @since 2011/03/16 * */ public class BatteryActivity extends Activity implements OnClickListener{ // 当系统处于充电状态或者电量发生改变时会广播该action private static final String ACTION_NAME = Intent.ACTION_BATTERY_CHANGED; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.battery); Button button = (Button) findViewById(R.id.button1); // 注意哦,我们这里的activity实现了View.OnClickListener接口 button.setOnClickListener(this); } // 声明广播接收者 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String actionName = intent.getAction(); if(actionName.equals(ACTION_NAME)){ int level = intent.getIntExtra("level", 0); int scale = intent.getIntExtra("scale", 100); // Toast.makeText(BatteryActivity.this, "level="+level+" scale="+scale, Toast.LENGTH_LONG).show(); final Dialog dialog = new Dialog(BatteryActivity.this); dialog.setTitle("电量显示"); // 加载xml布局文件为View对象 LayoutInflater flater = LayoutInflater.from(BatteryActivity.this); View view = flater.inflate(R.layout.battery2, null); dialog.setContentView(view,new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); /** * 获取该dialog显示的父窗口,然后设置当dialog显示时,窗口内的其他部分设置为模糊 * WindowManager.LayoutParams.FLAG_BLUR_BEHIND告诉我们,无论什么对象显示于前段, * 都会显示在window的最上层 */ Window window = dialog.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND); /** * 这里注意findViewById()方法,默认的会去加载main.xml中的组件。如果你想加载x.xml中的id为 * a的组件,那么应该在前面加上setContentView这个布局文件的对象,如这里的dialog */ TextView textView = (TextView)dialog.findViewById(R.id.textView_message); textView.setText("目前的手机电量为"+level*100/scale+"%"); Button button2 = (Button) dialog.findViewById(R.id.button_back); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); // 注销广播接收者 unregisterReceiver(broadcastReceiver); } }); dialog.show(); } } }; @Override public void onClick(View v) { IntentFilter filter = new IntentFilter(ACTION_NAME); registerReceiver(broadcastReceiver, filter); } }

battery2.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:text="TextView" android:id="@+id/textView_message" android:textSize="10sp" android:gravity="center" android:padding="10px" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView> <Button android:id="@+id/button_back" android:layout_width="wrap_content" android:text="返回" android:layout_height="wrap_content"></Button> </LinearLayout>

battery.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:text="显示电量" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>

更多相关文章

  1. android 仿淘宝的加载刷新效果
  2. android WebView加载URL不显示图片
  3. 个性进度条--------菊花加载----Android 播放动画
  4. android 自定义view 类似loadding从下往上加载的动画
  5. android webView判断是否加载完成的2种办法
  6. Android里面WebView加载HTML里面点击按钮调我Android方法
  7. Android电池电量更新 - BatteryService(转)
  8. android图片异步加载 列表

随机推荐

  1. android-获取手机电话的状态
  2. Android导入源码
  3. 调用Android系统设置中的Intent
  4. Android中WebView使用规范
  5. Android中使用开源框架Citypickerview实
  6. Android常用布局组件----重新认识Android
  7. 第99章、Android调用Javascript(从零开始
  8. android内部培训视频_第五节(1)_OA实战之
  9. Android输入管理服务启动过程源码分析
  10. 3.1如何编写程序界面&3.2常见控件使用方