2

How can my app play a short sound every hour, even when it is not running in the foreground?

Try-1: I tried a handler, but this does not help.

Try-2: as @Marcin, suggested, I tried the AlarmManager. No sound is played when the mobile is inactive. When I open the app, then it plays the sound.

Try-3: Add a notification channel with high priority on it. This plays the sound at least once, even when the mobile is standby. Swiping the notification away won't play the sound again. Setting the notification autoCancel to false won't help.

private void setAlarm() {
    alarmManager = (AlarmManager) getSystemService( Context.ALARM_SERVICE);
    Intent intent = new Intent( this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast( this, 0, intent, FLAG_IMMUTABLE);
    alarmManager.setInexactRepeating( AlarmManager.RTC_WAKEUP,
            LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
            60000L, pendingIntent);
    createNotificationChannel();
}

private void createNotificationChannel() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "My ReminderChannel";
        String description = "Channel for the Alarm manager";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel( "myandroid", name, importance);
        channel.setDescription( description);
        NotificationManager notificationManager = getSystemService( NotificationManager.class);
        notificationManager.createNotificationChannel( channel);
    }
}

The AlarmReceiver is:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent( context, MainActivity.class);
        intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, i, PendingIntent.FLAG_IMMUTABLE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "myandroid")
                .setSmallIcon(R.drawable.add_checklist_item)
                .setContentTitle("Wakeup")
                .setContentText("Descsription")
                .setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent( pendingIntent);
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(12345, builder.build());
        playRingtone();
    }

    public void playRingtone() {
        try {
            MediaPlayer mp = MediaPlayer.create(MainActivity.mainActivity, R.raw.tock);
            mp.start();
            mp.setOnCompletionListener(MediaPlayer::release);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In the AndroidManifest.xml is:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>

    <receiver android:name="com.example.myapp.AlarmReceiver"/>
</application>

Of course I don't want my silent app draining my battery.

tm1701
  • 7,307
  • 17
  • 79
  • 168

1 Answers1

1

Use workmanager. here is the link.

Saiful Sazib
  • 451
  • 1
  • 3
  • 14