0

so, advance note: I have not worked with the Looper before, neither with Notifications or AlarmManager, though i got some very basic understanding from Tutorials.

So i am currently programming an alarm App and am a bit torn between the possibilities of scheduling Alarms, being the following: 1. Using AlarmManager with NotificationCompat.Builder and PowerManager.WakeLock to assure the alarm goes off, even though, i don't know, how i could then implement the repeated playing of the alarmsound, which is stored as the String of its uri. The problem is, that the alarm should be able to be set, so it does only stop, if the user completes a task, that is previously set, like solving equations, a different location, scanning a qrcode, .... Also the alarm should increase the volume of the sound to max volume over a settable period of time after going off and should be able to vibrate, if set. Since i don't have knowledge with this, is this implementable, using the above? If yes how? If no: 2. Using the Looper to queue the functionality of the alarm to its time. I saw a code snippet using something like a delayed call or something like that. It seems, this is rather able to conduct the job of what i described in 1., but if not, please proove me wrong. If it is, i have heard, that the application needs to be running in the background, so that the looper goes off. Is this true? How can i make sure, that the alarm is still functioning, if someone accidently closes the app, or has something like a limitation of background apps, that automatically kills my app? Maybe this isn't a problem and i am just too worried about it, in that case, also please tell me.

I am glad for any constructive help :)

So far, this is what i worked with, though somehow it caused an infinite loop to happen. I'll share it here, so you can see what i have tried, but i'd like to get a better understanding of AlarmManager/Looper before trying to debug this.

package com.example.alarm;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.PowerManager;

import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import java.io.IOException;
import java.util.ArrayList;

public class AlarmReceiver extends BroadcastReceiver {

    int id = -1;

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

        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp:AlarmLock");
        wakeLock.acquire(30*60*1000L /*30 minutes*/);

        playSound(context);

        Intent i = new Intent(context, ActiveAlarmActivity.class);
        id = intent.getIntExtra("id", -1);
        i.putExtra("id", id);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_IMMUTABLE); //todo not sure, if flagImmutable is right here

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "AlarmChannelID")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("Alarm Manager")
                .setContentText("Alarm")
                .setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(123, builder.build());

        wakeLock.release();


    }

    private void playSound(Context context) {

        Alarm alarm = new DBHelper(context, "Database.db").getAlarm(id);
        Uri uri = Uri.parse(alarm.getSoundPath(-1));

        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        // Request audio focus
        int result = audioManager.requestAudioFocus(
                null,
                AudioManager.STREAM_ALARM,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT
        );

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            try {
                // Play the sound
                MediaPlayer mediaPlayer = new MediaPlayer();

                try{
                    mediaPlayer.setDataSource(context, uri);
                    mediaPlayer.prepare();
                }catch (IOException e){
                    e.printStackTrace();
                }

                // Release audio focus after playback is complete
                mediaPlayer.setOnCompletionListener(mp -> {
                    audioManager.abandonAudioFocus(null);
                    mediaPlayer.release();
                });

                mediaPlayer.start();
                mediaPlayer.setLooping(true);



            } catch (Exception e) {
                // Handle sound playback errors
                e.printStackTrace();
                audioManager.abandonAudioFocus(null);
            }
        }
    }






}

0 Answers0