显示电池电量

先修改res/values/colors.xml和res/values/string.xml

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="blue">#0000FF</color>
</resources>

res/values/string.xml

<resources>
    <string name="app_name">Ex.GetEnergy</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="str_title">如何取得电池计量?</string>
    <string name="str_button1">按我取得电池计量</string>
    <string name="str_button2">返回</string>
    <string name="str_dialog_title">系统信息</string>
    <string name="str_dialog_body">电池计量:</string>
</resources>

activity:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends Activity {
    private int intLevel;
    private int intScale;
    private Button mButton01;
    private AlertDialog d;
    private final BroadcastReceiver mBatInfoReceiver=new BroadcastReceiver()
    {
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if (Intent.ACTION_BATTERY_CHANGED.equals(action))
            {
                intLevel = intent.getIntExtra("level", 0);
                intScale = intent.getIntExtra("scale", 100);
                onBatteryInfoReceiver(intLevel,intScale);
            }
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton01 = findViewById(R.id.myButton1);
        mButton01.setOnClickListener(v -> registerReceiver
                (
                        mBatInfoReceiver,
                        new IntentFilter(Intent.ACTION_BATTERY_CHANGED)
                ));
    }
    public void onBatteryInfoReceiver(int intLevel, int intScale)
    {
        d = new AlertDialog.Builder(MainActivity.this).create();
        d.setTitle(R.string.str_dialog_title);
        d.setMessage(getResources().getString(R.string.str_dialog_body)+
                intLevel * 100 / intScale + "%");
        d.setButton(DialogInterface.BUTTON_POSITIVE, getResources().getString(R.string.str_button2),
                (dialog, which) -> {
                    unregisterReceiver(mBatInfoReceiver);
                    d.dismiss();
                });
        d.show();
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="@string/str_title"
        android:textColor="@color/blue"
        android:textSize="20sp" />

    <Button
        android:id="@+id/myButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/myTextView1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="56dp"
        android:layout_marginTop="71dp"
        android:text="@string/str_button1"
        android:textColor="@color/black"
        android:textSize="14sp" />

</LinearLayout>
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值