通过 AlarmManager 设置的警报不起作用
alarm set through AlarmManager isn't working
我正在创建一个应用程序,如果 google chrome 使用时间过长,我会尝试推送通知。我正在使用可访问性服务来检测正在使用哪个应用程序,并且我已经根据我对何时推送通知的看法编写了条件。它完美地工作到这里。然后我尝试使用 AlarmListener 设置警报,这就是问题所在。作为参数传递给 AlarmListener 的 PendingIntent(触发前台服务,前台服务又触发通知)不起作用。请告诉我哪里出错了:
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());
}
}
编辑:
我现在使用广播接收器而不是前台服务。这仍然不起作用。
我尝试了很多方法,但最终解决我问题的方法是使用 AlarmManager.setAlarmClock 方法...
我正在创建一个应用程序,如果 google chrome 使用时间过长,我会尝试推送通知。我正在使用可访问性服务来检测正在使用哪个应用程序,并且我已经根据我对何时推送通知的看法编写了条件。它完美地工作到这里。然后我尝试使用 AlarmListener 设置警报,这就是问题所在。作为参数传递给 AlarmListener 的 PendingIntent(触发前台服务,前台服务又触发通知)不起作用。请告诉我哪里出错了:
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());
}
}
编辑: 我现在使用广播接收器而不是前台服务。这仍然不起作用。
我尝试了很多方法,但最终解决我问题的方法是使用 AlarmManager.setAlarmClock 方法...