位置: 编程技术 - 正文
推荐整理分享Android实时获取电量(android 获取时区),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:android 获取手机信息,android 获取时区,android实时获取音频数据,android获取数据并显示,android实时获取微信聊天信息,android 获取时区,android 获取当前时间,android实时获取音频数据,内容如对您有帮助,希望把文章链接给更多的朋友!
转载自: class MainActivity extends Activity { class BtteryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub System.out.println(""); // 判断它是否是为电量变化的Broadcast Action if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) { // 获取当前电量 int level = intent.getIntExtra("level", 0); // 电量的总刻度 int scale = intent.getIntExtra("scale", 0); // 把它转换成百分比 String content = ("电池电量为" ((level * ) / scale) "%"); int key = ((level * ) / scale); switch (key) { case : Toast.makeText(context, "电池已经充满", Toast.LENGTH_LONG).show(); break; case : Toast.makeText(context, "电池已经使用一半", Toast.LENGTH_LONG) .show(); break; case : Toast.makeText(context, "电池已到%了", Toast.LENGTH_LONG) .show(); break; case : Toast.makeText(context, "电池已到%了", Toast.LENGTH_LONG) .show(); break; case : Toast.makeText(context, "电池不足%", Toast.LENGTH_LONG) .show(); break; case 9: Toast.makeText(context, "电池不足9%", Toast.LENGTH_LONG).show(); break; case 5: Toast.makeText(context, "电池不足5%", Toast.LENGTH_LONG).show(); break; } } } }
另一篇博客
Android开发者可以利用BroadcastReceiver机制,获取电池电量变化的ACTION_BATTERY_CHANGED intent,进而获取当前android设备的电池状态。具体操作步骤:1. 创建一个监听ACTION_BATTERY_CHANGED事件的intentFilter。2. 创建一个BroadcastReceiver对象,该对象可以接收broadcast intent。3. 注册BroadcastReceiver对象来监听ACTION_BATTERY_CHANGED事件。4. 在BroadcastReceiver对象中,重写onReceive方法,在onReceive方法的传入参数intent里获取需要的电池状态信息。5. 在生命周期结束时,取消step4中的注册。
[java]view plaincopyprint?public class BatteryMonitorActivity extends Activity { private TextView batterLevel; private BroadcastReceiver batteryLevelRcvr; private IntentFilter batteryLevelFilter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); batterLevel = (TextView) findViewById(R.id.batteryLevel); monitorBatteryState(); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(batteryLevelRcvr); } private void monitorBatteryState() { batteryLevelRcvr = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { StringBuilder sb = new StringBuilder(); int rawlevel = intent.getIntExtra("level", -1); int scale = intent.getIntExtra("scale", -1); int status = intent.getIntExtra("status", -1); int health = intent.getIntExtra("health", -1); int level = -1; // percentage, or -1 for unknown if (rawlevel >= 0 && scale > 0) { level = (rawlevel * ) / scale; } sb.append("The phone"); if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health) { sb.append("'s battery feels very hot!"); } else { switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: sb.append("no battery."); break; case BatteryManager.BATTERY_STATUS_CHARGING: sb.append("'s battery"); if (level <= ) sb.append(" is charging, battery level is low" "[" level "]"); else if (level <= ) sb.append(" is charging." "[" level "]"); else sb.append(" will be fully charged."); break; case BatteryManager.BATTERY_STATUS_DISCHARGING: case BatteryManager.BATTERY_STATUS_NOT_CHARGING: if (level == 0) sb.append(" needs charging right away."); else if (level > 0 && level <= ) sb.append(" is about ready to be recharged, battery level is low" "[" level "]"); else sb.append("'s battery level is" "[" level "]"); break; case BatteryManager.BATTERY_STATUS_FULL: sb.append(" is fully charged."); break; default: sb.append("'s battery is indescribable!"); break; } } sb.append(' '); batterLevel.setText(sb.toString()); } }; batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryLevelRcvr, batteryLevelFilter); } } [html]view plaincopyprint?<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@id/batteryLevel" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal" android:textSize="dip"> </TextView> </LinearLayout>Android的上层实现显示手机电池电量是通过监听实现,当电池电量改变时会通知到程序。当然这个有朋友就要问了我不是想监听电池电量,我是想直接获取当前的电池电量。其实这是同一个问题,当你在初始化监听器的时候他会自动首先获取电池的电量信息,然后后续当电池改变时同时也会通知当前电池电量的改变情况。
转自
Android实时获取网络信号 转载自:
Android权限大全 转载自:
反编译Android APK 反编译APK,主要是为了学习其它APK中优秀的设计部分。我们在编程的时候,有时候看到其它APK中不错的东西,想知道它的做法,就可以去反编译它来学
标签: android 获取时区
本文链接地址:https://www.jiuchutong.com/biancheng/387512.html 转载请保留说明!友情链接: 武汉网站建设