这次邮件我们将会讨论怎么获取电量状态在安卓设备上,为了完成这个目标,我们将会使用到广播。
What is BroadcastReceiver?
A broadcast receiver is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android application.
The system itself broadcast event all time, as example when system gets booted or an SMS arrived, etc.
Application description
Application display of battery level and update this value when battery level changed.
在Android Studio 中新建一个工程
在String.xml中添加一个常量
-
<string name="battery_level">Battery level:</string>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:text="@string/battery_level" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_marginTop="8dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:max="100" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_toRightOf="@+id/textView" android:layout_toEndOf="@+id/textView" /> </LinearLayout>
创建一个内部类为我们的广播实现
private class BatteryBroadcastReceiver extends BroadcastReceiver { private final static String BATTERY_LEVEL = "level"; @Override public void onReceive(Context context, Intent intent) { int level = intent.getIntExtra(BATTERY_LEVEL, 0); mBatteryLevelText.setText(getString(R.string.battery_level) + " " + level); mBatteryLevelProgress.setProgress(level); } }
更新Activity的代码如下:
public class MainActivity extends Activity { private TextView mBatteryLevelText; private ProgressBar mBatteryLevelProgress; private BroadcastReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBatteryLevelText = (TextView) findViewById(R.id.textView); mBatteryLevelProgress = (ProgressBar) findViewById(R.id.progressBar); mReceiver = new BatteryBroadcastReceiver(); registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); } @Override protected void onStop() { unregisterReceiver(mReceiver); super.onStop(); } private class BatteryBroadcastReceiver extends BroadcastReceiver { private final static String BATTERY_LEVEL = "level"; @Override public void onReceive(Context context, Intent intent) { int level = intent.getIntExtra(BATTERY_LEVEL, 0); mBatteryLevelText.setText(getString(R.string.battery_level) + " " + level); mBatteryLevelProgress.setProgress(level); } } }
我们必须反注册你的接收者当activity 停止,因为如果不反注册,当你的应用程序关闭时候,进程会一直在后台接受一些信息
注册广播:
We can register receiver in AndroidManifest.xml: <receiver android:name="BatteryBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.BATTERY_CHANGED" /> </intent-filter> </receiver>
or register our BroadcastReceiver in application BatteryBroadcastReceiver mReceiver = new BatteryBroadcastReceiver(); registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
运行程序
We can change battery level of our emulator use console. We must connect to out AVD Emulator use console / terminal. If we run many emulator, each emulator will have unique port. Port number your emulator you can find in title of emulator window. As example port of my emulator is 5554.
Connect to emulator:
- telnet localhost <PORT>
Change battery level of emulator:
- power capacity <BATTERY_LEVEL_IN_PERCENT>
Next step is change battery level on emulator.
As you can see TextView and ProgressBar were updated.