Naxsami
Naxsami

Reputation: 133

Why my low battery alert code didn't work?

i try make low battery alert but my code didn't work its always show Toast massage on all level even i set < 30 maybe somethink missing or my code is wrong? and also Toast will disappear when i click on screen even i set toast.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();
            }
        }

any idea to solve my problem?

Upvotes: 1

Views: 110

Answers (2)

Amirhf
Amirhf

Reputation: 398

To sum it up, a broadcast receiver for the 

ACTION_BATTERY_CHANGED

 intent is set up dynamically, because it can not be received through components declared in manifests, only by explicitly registering for it with 

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)); } }

Upvotes: 0

Laura &#193;lvarez
Laura &#193;lvarez

Reputation: 54

I guess that you are trying to make a Low Battery Alarm, and show a Toast when the battery level is lower than 30%.

Your progress bar should work ok, but in the documentation of the BatteryManager you can see that the constant BatteryManager.EXTRA_LEVEL is equals to 'level' and no 'Level', so your code is not getting the correct value.

For getting the Toast to work, you must modify the line:

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

with this one

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

(UPDATE)

This way, the Toast will be shown every time that your battery is lower than the threshold (for instance, when it is 29, 28, 27...). To fix it, a simple way is something like this:

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;
        }

Upvotes: 1

Related Questions