I am creating an application where I am trying to push a notification if google chrome is being used for too long. I am using accessibility service to detect which application is being used and I have written conditionals according to my vision of when the notification should be pushed. It works perfectly till here. Then I have tried to set an alarm using AlarmListener and this is where the problem lies. The PendingIntent(which triggers a foreground service which in turn fires the notification) passed as an argument to AlarmListener doesn't work. Please tell me where I am going wrong:
ApplicationListener.java
public class ApplicationListenerService extends AccessibilityService {
public NotificationCompat.Builder createNotification(String title, String content, String channel_id, int priority) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(content)
.setPriority(priority);
return builder;
}
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
String packageName = accessibilityEvent.getPackageName().toString();
if (!packageName.equals("com.android.systemui")) {
SharedPreferences sharedPref = this.getSharedPreferences(
"ApplicationListener", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String storedPackage = sharedPref.getString("current_running_application", "none");
if (storedPackage.equals("none")) {
if (packageName.equals("com.android.chrome")) {
//instead of chrome check for all blacklisted apps
Log.d("obscure_tag", "chrome has been detected for the first time...starting alarm");
editor.putString("current_running_application", packageName);
editor.apply();
AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmMgr.setRepeating(AlarmManager.RTC,
System.currentTimeMillis(), 60000,
alarmIntent);
}
} else if (storedPackage.equals(packageName)) {
Log.d("obscure_tag", "chrome has been detected for the second time..doing nothing");
} else {
//delete alarm and stored package
Log.d("obscure_tag", "different app is detected...alarm getting cancelled...");
editor.clear();
editor.apply();
AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmMgr.cancel(alarmIntent);
}
}
}
@Override
public void onInterrupt() {
Log.d("obscure_tag", "service is interrupted");
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
info.notificationTimeout = 100;
this.setServiceInfo(info);
}
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
public NotificationCompat.Builder createNotification(Context context, String title, String content, String channel_id, int priority) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel_id)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(content)
.setPriority(priority);
return builder;
}
@Override
public void onReceive(Context context, Intent arg1) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, createNotification(context, "Demo task", "this task is incomplete", "task_reminders", 4).build());
}
}
EDIT: I have made used a broadcast receiver instead of a foreground service right now. Still this isn't working.
I tried a lot of things but what finally solved my problem is using AlarmManager.setAlarmClock method...