Using AlarmManager to activate and deactivate silence mode

亡梦爱人 提交于 2019-12-11 06:13:32

问题


In my program, I am getting 2 dates from user. First date is for activating silence mode and the second date is deactivating it. For handling this issue, I have tried to use 2 different AlarmManager and 2 different BroadcastReceiver but I couldn't achieve it. I can activate silence mode at the first date but I couldn't deactivate it. This a part of my code:

public class AddEvent extends AppCompatActivity {
    private static PendingIntent silenceActivatorPendingIntent;
    private static PendingIntent silenceDeactivatorPendingIntent;
    private static AlarmManager manager1;
    private static AlarmManager manager2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent silenceActivatorIntent = new Intent(this, SilenceModeActivator.class);
        Intent silenceDeactivatorIntent = new Intent(this, SilenceModeDeactivator.class);
        silenceActivatorPendingIntent = PendingIntent.getBroadcast(this, 0, silenceActivatorIntent, 0);
        silenceDeactivatorPendingIntent = PendingIntent.getBroadcast(this, 0, silenceDeactivatorIntent, 0);
        manager2 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        manager1 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
   }

   public void addEvent(View view) {
       GregorianCalendar targetStartDate = new GregorianCalendar(startYear, startMonth, startDay, startHour, startMinute, 0);
       GregorianCalendar targetEndDate = new GregorianCalendar(endYear, endMonth, endDay, endHour, endMinute, 0);
       manager1.set(AlarmManager.RTC_WAKEUP, targetStartDate.getTimeInMillis(), silenceActivatorPendingIntent);
       manager2.set(AlarmManager.RTC_WAKEUP, targetEndDate.getTimeInMillis(), silenceDeactivatorPendingIntent);
   }

   public static void stopAlarm1() {
    manager1.cancel(silenceActivatorPendingIntent);
   }

   public static void stopAlarm2() {
    manager2.cancel(silenceDeactivatorPendingIntent);
  }
}

addEvent is my button clicker method.

Silence Mode Activator:

public class SilenceModeActivator extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
    activateSilentMode(context);
    AddEvent.stopAlarm1();
}

public void activateSilentMode(Context context) {

    NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Check if the notification policy access has been granted for the app.
    if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
        Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
        context.startActivity(intent);
    }

    mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
}
}

Silence Mode Deactivator:

public class SilenceModeDeactivator extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    deactivateSilentMode(context);
    AddEvent.stopAlarm2();
}

public void deactivateSilentMode(Context context) {

    NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Check if the notification policy access has been granted for the app.
    if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
        Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
        context.startActivity(intent);
    }

    mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
}
}

Do you have any idea about how I can fix this? Thanks in advance.

来源:https://stackoverflow.com/questions/41280319/using-alarmmanager-to-activate-and-deactivate-silence-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!