我可以将警报更改为重复警报吗?

2020年9月25日 37点热度 0条评论

如何将allreaddy设置的警报更改为重复警报?
我用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);

感谢您的帮助,

弗洛里安

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

解决方案如下:

您已经按Service类完成了这项工作。

例如

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;
    }
}