如何在java android中创建多个警报管理器?与使用服务

问题描述 投票:0回答:1

当我尝试在应用程序中创建两个或三个或更多警报时只有一个警报正在工作!

我需要在我的服务中创建警报管理器来发出两个或更多警报。

这是启动 ForegroundService (MyService.java) 的文件一:

    public void setAlarm() {
        MyService.year = year;
        MyService.month = month;
        MyService.day = day;
        MyService.hour = hour;
        MyService.minute = minute;

        Intent intent = new Intent(getApplicationContext(), MyService.class);
        startForegroundService(intent);
    }

这是文件二 (MyService.java):

    static String year;
    static String month;
    static String day;
    static int hour;
    static int minute;
    public static final String CHANNEL_ID = "ForegroundServiceChannel";
    static AlarmManager manager;
    static PendingIntent pendingIntent;
    static List<PendingIntent> pendingIntents;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, DefinedActivities.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("app")
                .setContentText("my app is running")
                .setSmallIcon(es.dmoral.toasty.R.drawable.ic_info_outline_white_24dp)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);
        start();
        return START_STICKY;
    }
    public void start() {
        Calendar calNow = Calendar.getInstance();
        Calendar calendar = (Calendar) calNow.clone();
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);

        Random random = new Random();

        int ticks = random.nextInt(6000);
        pendingIntents = new ArrayList<>();

        Intent intent2 = new Intent(getApplicationContext(), Tasks.class);
        pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), ticks, intent2, PendingIntent.FLAG_IMMUTABLE);

        manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        pendingIntents.add(pendingIntent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void createNotificationChannel() {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
    }
    public static void stopAlarm(int RQS){
        manager.cancel(pendingIntents.get(RQS));
        pendingIntents.remove(RQS);
    }

在 MyService.java 中,我尝试在警报开始时转到 Tasks.java 并运行所需的所有任务。

这是Tasks.java:

    MyDatabaseHelper myDB;
    Cursor result;
    List<String> activitiesList;
    List<String> idsList;

    @Override
    public void onReceive(Context context, Intent intent) {

        myDB = new MyDatabaseHelper(context);

        activitiesList = new ArrayList<>();
        idsList = new ArrayList<>();
        result = myDB.showAllData();

        while (result.moveToNext()) {
            StringBuilder activity = new StringBuilder();

            String id = result.getString(0);
            activity.append(result.getString(6));

            activitiesList.add(String.valueOf(activity));
            idsList.add(String.valueOf(id));
        }

        for (String i : activitiesList) {
            switch (i) {
                case "Silent":
                    silent(context);
                    clear();
                    break;
                case "Normal sound":
                    normal(context);
                    clear();
                    break;
                case "Vibrate":
                    vibrate(context);
                    clear();
                    break;
            }
        }
    }

    public void clear() {
        activitiesList.remove(0);
        myDB.deleteData(idsList.get(0));
        idsList.remove(0);
        MyService.stopAlarm(0);
    }

    public void silent(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        Toasty.success(context, "Silent!", Toast.LENGTH_LONG).show();
    }

    public void normal(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        Toasty.success(context, "Normal Volume!", Toast.LENGTH_LONG).show();
    }

    public void vibrate(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        Toasty.success(context, "Vibrated!", Toast.LENGTH_LONG).show();
    }

我的问题是什么?

谢谢!

java android service broadcastreceiver alarmmanager
1个回答
0
投票

我找到了我的解决方案:WorkManager

我们可以使用工作管理器类来做到这一点...

© www.soinside.com 2019 - 2024. All rights reserved.