0

I am working on an Android Studio project that does some actions when the phone is connects to a power source. In order to do this, I made a broadcast receiver that listens for android.intent.action.BATTERY_CHANGED and launches a java activity. But it turns out my activity is never launched. Here is my code :

public class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "45441141645", Toast.LENGTH_LONG).show();
        //This toast here is never displayed



        boolean charging = false;
        Toast.makeText(context, "Tried", Toast.LENGTH_LONG).show();
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean batteryCharge = status == BatteryManager.BATTERY_STATUS_CHARGING;

        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        if (batteryCharge) charging=true;
        if (usbCharge) charging=true;
        if (acCharge) charging=true;

        if (charging) Toast.makeText(context, "Battery Charging", Toast.LENGTH_LONG).show();;


    }


}

And a part of my manifest :

<receiver android:name=".Receiver"  android:exported="true" android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BATTERY_CHANGED"/>
            </intent-filter>

        </receiver>
    </application>

I also tried to put some Log.d in my code to see where the problem was coming from but I didn't figure out anything

Any help is much appreciated

ScarySneer
  • 199
  • 3
  • 9

0 Answers0