在电池处于定义水平时尝试做某事时得到 BATTERY_CHANGED flg=0x60000010(有额外的)

getting BATTERY_CHANGED flg=0x60000010 (has extras) when trying to do something when battery is at a defined level

我试图在电池处于定义的水平时更改 TextView 的文本,但是当发生这种情况时我却收到了错误,错误是

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in famium.fokus.fhg.de.monitoringthebatterylevelandchargingstate.MainActivity@4139cca8

这是我的代码,我在导致错误的行之前进行了评论。

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        /*
            BatteryManager
                The BatteryManager class contains strings and constants used for values in the
                ACTION_BATTERY_CHANGED Intent, and provides a method for querying battery
                and charging properties.
        */
        /*
            public static final String EXTRA_SCALE
                Extra for ACTION_BATTERY_CHANGED: integer containing the maximum battery level.
                Constant Value: "scale"
        */
        // Get the battery scale
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
        // Display the battery scale in TextView
        mTextViewInfo.setText("Battery Scale : " + scale);

        /*
            public static final String EXTRA_LEVEL
                Extra for ACTION_BATTERY_CHANGED: integer field containing the current battery
                level, from 0 to EXTRA_SCALE.

                Constant Value: "level"
        */
        // get the battery level
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
        // Display the battery level in TextView
        mTextViewInfo.setText(mTextViewInfo.getText() + "\nBattery Level : " + level);

        // Calculate the battery charged percentage
        float percentage = level/ (float) scale;
        // Update the progress bar to display current battery charged percentage
        mProgressStatus = (int)((percentage)*100);

        // Show the battery charged percentage text inside progress bar
        mTextViewPercentage.setText("" + mProgressStatus + "%");

        // Show the battery charged percentage in TextView
        mTextViewInfo.setText(mTextViewInfo.getText() +
                "\nPercentage : "+ mProgressStatus + "%");

        // Display the battery charged percentage in progress bar
        mProgressBar.setProgress(mProgressStatus);

        String action = intent.getAction();
        if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
            IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            Intent batteryStatus = context.registerReceiver(null, ifilter);

            int level1 = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale1 = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
            int percent = (level1*100)/scale1;
 // here the lines that causes the error
            if (percent >= 97) {
                _textView2.setText(percent);
            }
        }
    }
};

我试着按照这个 post 但我不知道哪里出了问题。

看完这篇我明白了post

所以,而不是

_textView2.setText(percent);

改为

_textView2.setText("" + percent);