我可以将闹钟更改为重复闹钟吗?

can i change an alarn to a repeating alarm?


我怎样才能将一个已经设置好的闹钟改为重复闹钟? 我用 AlarmManager 设置闹钟? 我的实际代码如下所示:

    intent = new Intent(MainActivity.this, AlarmReceiverActivity.class);
                        pendingIntent = PendingIntent.getActivity(MainActivity.this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, time, intervall, pendingIntent);

感谢您的帮助,
弗洛里安

编辑:我用另一种方式解决了我的问题(每次都设置一个新闹钟),所以没有压力,但我仍然想知道这将如何工作

您已通过服务 class 完成了这项工作。

例如

Application's onCreate() 方法中,您必须像下面这样初始化 AlarmManager

long l = 60000;
try {

            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

            Intent intent = new Intent(context, SyncBroadcastReceiver.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, SYNC_CALL_TO_SERVER_PENDING_INTENT,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), l, pendingIntent);

        }catch (Exception e){
            e.printStackTrace();

        }

并且SyncBroadcastReceiver喜欢,

public class SyncBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            //comment to check crash
            Intent intent1 = new Intent(context, ServiceClass.class);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(intent1);
            } else {
                context.startService(intent1);
            }
        } catch (Exception e){
            e.printStackTrace();

        }
    }
}

和 ServiceClass 一样

public class ServiceClass extends Service {
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         // hitNotification(); // what need to act like notification dialog
         return START_NOT_STICKY;
    }
}