0

Hello I am new to android and I am trying to make an app that will check battery status when device is charging . if battery level is more than defined in setting than it will show a notification and will also ring .

I am now just testing it and showing notification after every 30 second after device is plugged in . according my logic alarm manger will set when device is plugged in and will cancel pending of alarm manager when device is plugged out.

But it is not working, notification is also showing after disconnecting device . I don't know why it is happening . I have tried to cancel pending but it is not working.

please help me what is going on here.

CustomReceiver

public class CustomReceiver extends BroadcastReceiver {

    AlarmManager alarmManager;
    PendingIntent p;

    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        if (intentAction != null) {
            if (intentAction.equals(Intent.ACTION_POWER_CONNECTED)) {
                toast("Power Connected", context);
                setBatteryAlarm(context);
            }
            if (intentAction.equals(Intent.ACTION_POWER_DISCONNECTED)) {
                toast("Power Disconneted", context);
                cancelAlarm();
            }
        }
    }


    public void toast(String msg, Context context) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    private void setBatteryAlarm(Context context) {
        Intent notifyIntent = new Intent(context, BatteryCheckingReciever.class);
        p = PendingIntent.getBroadcast(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        long repeatInterval = 30000;
        long triggerTime = SystemClock.elapsedRealtime() + repeatInterval;

        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerTime, repeatInterval, p);
    }

    private void cancelAlarm() {
        Log.e("kiran", "canceling alarm");
        if (alarmManager != null) {
            alarmManager.cancel(p);
            alarmManager = null;
        }
    }
}

BroadCast reciever for notification

public class BatteryCheckingReciever extends BroadcastReceiver {

    NotificationManager notificationManager;
    int NOTIFICATION_ID = 0;
    String PRIMARY_CHANNEL = "battery_is_full";

    @Override
    public void onReceive(Context context, Intent intent) {

        showNotificaiton(context);
    }

    private void showNotificaiton(Context context) {
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, PRIMARY_CHANNEL)
                .setSmallIcon(R.drawable.ic_battery_full)
                .setContentTitle(context.getString(R.string.battery_full))
                .setContentText(context.getString(R.string.battery_full_text))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL);

        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

manifest file

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".BatteryCheckingReciever"
            android:enabled="true"
            android:exported="true"></receiver>
        <receiver
            android:name=".CustomReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

sorry for bad English .

Akshay
  • 2,506
  • 4
  • 34
  • 55
Kiran Patel
  • 98
  • 3
  • 12

1 Answers1

0

it is because your reference for alarm manager and pending intent change. Most probably your alarm manager becomes null but because you are making null check you dont have error. You need to reference same objects again

Change your code like this:

  private void cancelAlarm() {

 alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent notifyIntent = new Intent(context, BatteryCheckingReciever.class);
 p = PendingIntent.getBroadcast(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        if (alarmManager != null) {
       Log.e("kiran", "canceling alarm");
            alarmManager.cancel(p);
            alarmManager = null;
        }
    }

https://stackoverflow.com/a/4350149/11982611

Eren Tüfekçi
  • 2,463
  • 3
  • 16
  • 35
  • as i was experimenting i thought to do something different means i set alarm in service which get start on ACTION_POWER_CONNECTED and broadcast receiver stops that service on ACTION_POWER_DISCONNECTED . i also delivers on going notification on service start and also cancel that service and battery level gets check into other service but the problem is that alarm works fine when phone screen is on but alarm does not work when it is asleep or charging . i am using setInExactRepeating and also my device version is nogut. – Kiran Patel Jan 16 '20 at 05:14
  • use setExactAndAllowWhileIdle this one – Eren Tüfekçi Jan 16 '20 at 07:03
  • thank you for response but it is not working as intended , i tried to make app without alarm manager . concept is start service on power connection broadcast and in that service register broadcast for BATTERY_CHANGE . so whenever we get broadcast for battery_change we can check status of battery and it is working really good no need alarm manager at all . thank you – Kiran Patel Jan 18 '20 at 06:07