1. 程式人生 > >android 監聽電池電量的變化

android 監聽電池電量的變化

package com.example.test;

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.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {
    private int level;
    private int scale;
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                level = intent.getIntExtra("level", 0);
                scale = intent.getIntExtra("scale", 0);
                onBatteryInfoReceiver(level, scale);
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        findViewById(R.id.btn_battery).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            }
        });
    }

    public void onBatteryInfoReceiver(int level, int scale) {
        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.setTitle("電池電量");
        dialog.setContentView(R.layout.dialog_battery);

        //建立背景模糊的window,並將對話方塊放在前景
        Window window = dialog.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        TextView mTextView = (TextView) dialog.findViewById(R.id.tv_battery);
        mTextView.setText("電池剩餘電量為:" + level * 100 / scale + "%");

        dialog.findViewById(R.id.btn_battery).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unregisterReceiver(mReceiver);
                dialog.dismiss();
            }
        });

        dialog.show();
    }


}

---------------------------------------佈局檔案-----------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_horizontal"
              android:background="@android:color/white"
        >
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_battery"
            android:text="註冊電量監聽事件"
            />
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal"
        >
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_battery"
            android:text="取消註冊電量監聽事件"
            />

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_battery"
            />
</LinearLayout>

---------------------------------------AndroidManifest.xml----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.test"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10"/>
    <application android:label="@string/app_name">
        <activity android:name="MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>