为什么我的电池电量不足警报代码不起作用?

时间:2019-02-13 13:21:50

标签: java android

我尝试发出电量不足警报,但是我的代码无法正常工作,即使我设置了Toast,它也始终在所有级别上显示< 30消息,也许有些想不到或我的代码错误?并且即使我设置了Toasttoast.setDuration(Toast.LENGTH_LONG);也会在屏幕上消失吗?

public void onReceive(Context context, Intent intent) {
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int lowLevel = intent.getIntExtra("Level", 0);

            float percentage = level/ (float) scale;
            mProgressStatus = (int)((percentage)*100);
            mViewPercentage.setText("" + mProgressStatus + "%");
            mProgressBar.setProgress(mProgressStatus);

            if (lowLevel < 30){

                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.lowbattery_toast, (ViewGroup) findViewById(R.id.low_battery));
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.BOTTOM, 0, 10);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);

                toast.show();
            }
        }

有什么办法解决我的问题吗?

2 个答案:

答案 0 :(得分:1)

我猜您正在尝试发出电池电量不足警报,并在电池电量不足30%时显示Toast。

您的进度条应该可以正常工作,但是在BatteryManager的文档中,您可以看到常量 BatteryManager.EXTRA_LEVEL 等于'level'而不是'Level',因此您的代码未获得正确的值。

要使Toast正常工作,必须修改以下行:

 int lowLevel = intent.getIntExtra("Level", 0);

与此一起

 int lowLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

(更新)

这样,每次您的电池电量低于阈值时(例如,当电池电量为29、28、27 ...时),都会显示Toast。要修复它,一种简单的方法是这样的:

private static int previousLevel = 100;

public void onReceive(Context context, Intent intent) {
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int lowLevel = intent.getIntExtra("Level", 0);

            float percentage = level/ (float) scale;
            mProgressStatus = (int)((percentage)*100);
            mViewPercentage.setText("" + mProgressStatus + "%");
            mProgressBar.setProgress(mProgressStatus);

            if (lowLevel < 30 && previousLevel >= 30){

                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.lowbattery_toast, (ViewGroup) findViewById(R.id.low_battery));
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.BOTTOM, 0, 10);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);

                toast.show();
            }

            previousLevel = lowLevel;
        }

答案 1 :(得分:0)

总结起来,

的广播接收器
  

ACTION_BATTERY_CHANGED

意图是动态设置的,因为它不能通过清单中声明的​​组件来接收,只能通过向

进行显式注册来实现。
  

Context.registerReceiver()

public class Main extends Activity { private TextView batteryTxt; private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context ctxt, Intent intent) { int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); batteryTxt.setText(String.valueOf(level) + "%"); } }; @Override public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.main); batteryTxt = (TextView) this.findViewById(R.id.batteryTxt); this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); } }