Attempt to get app to work properly when in the background in newer versions of Android, with assistance from qwen3.6:35b. Testing TBD.
This commit is contained in:
1 parent
1786f03577
commit
3965aa6d88
5 files changed
+221
-154
No files matched your search
@@ -1,6 +1,15 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<!-- 🔊 Required to play sound while backgrounded & bypass Doze -->
|
||||||
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
|
<!-- Android 14 requires declaring foreground service types -->
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
|
||||||
|
|
||||||
|
<!-- ⏰ Android 12+ exact alarm permission (users can deny, code handles it gracefully) -->
|
||||||
|
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
@@ -8,6 +17,7 @@
|
|||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/InfiniteTimer"
|
android:theme="@style/InfiniteTimer"
|
||||||
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
|
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
|
||||||
|
|
||||||
<activity android:name=".MainActivity" android:exported="true">
|
<activity android:name=".MainActivity" android:exported="true">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
@@ -15,13 +25,14 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<!-- 🔔 Register the receiver that triggers the alarm response -->
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
|
<receiver android:name=".TimerReceiver"
|
||||||
|
android:exported="false" />
|
||||||
<service
|
|
||||||
android:name=".Alert"
|
<!-- 🎵 Transient service to play audio & hold WakeLock -->
|
||||||
|
<service android:name=".TimerForegroundService"
|
||||||
android:foregroundServiceType="mediaPlayback"
|
android:foregroundServiceType="mediaPlayback"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
</application>
|
|
||||||
|
|
||||||
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
@@ -1,125 +1,85 @@
|
|||||||
package com.hyperling.apps.infinitetimer;
|
package com.hyperling.apps.infinitetimer;
|
||||||
|
|
||||||
import android.app.Notification;
|
import android.app.AlarmManager;
|
||||||
import android.app.NotificationChannel;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.media.MediaPlayer;
|
|
||||||
import android.media.RingtoneManager;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
import android.provider.Settings;
|
||||||
import android.os.IBinder;
|
|
||||||
import android.os.Looper;
|
|
||||||
import android.support.v4.app.NotificationCompat;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class Alert extends Service {
|
public class Alert {
|
||||||
|
private static final String TAG = "Alert";
|
||||||
|
public static final String ACTION_TIMER = "com.hyperling.apps.infinitetimer.ACTION_TIMER";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a persistent alarm that triggers the TimerReceiver.
|
||||||
|
* Uses setExactAndAllowWhileIdle to bypass Android Doze mode.
|
||||||
|
*/
|
||||||
|
public static void start(Context context, long intervalMs) {
|
||||||
|
Intent intent = new Intent(context, TimerReceiver.class);
|
||||||
|
intent.setAction(ACTION_TIMER);
|
||||||
|
|
||||||
boolean DEBUG = true;
|
// FLAG_IMMUTABLE required for Android 12+ (API 31+)
|
||||||
String TAG = "Alert.", tag;
|
int flags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT;
|
||||||
private static final String CHANNEL_ID = "alert_service_channel";
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
private static final int NOTIFICATION_ID = 1;
|
flags |= PendingIntent.FLAG_IMMUTABLE;
|
||||||
|
|
||||||
Uri ringtoneUri;
|
|
||||||
private MediaPlayer mediaPlayer;
|
|
||||||
private Handler handler = new Handler(Looper.getMainLooper());
|
|
||||||
private long intervalMs; // user-defined frequency
|
|
||||||
|
|
||||||
private final Runnable alertRunnable = new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
playAlert();
|
|
||||||
handler.postDelayed(this, intervalMs);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
||||||
tag = TAG + "onStartCommand";
|
|
||||||
|
|
||||||
if (intent != null) {
|
|
||||||
intervalMs = intent.getLongExtra("interval_ms", 5000L);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createNotificationChannel();
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, flags);
|
||||||
startForeground(NOTIFICATION_ID, buildNotification());
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
|
||||||
// Start the alert loop (your original Runnable logic)
|
if (alarmManager != null) {
|
||||||
handler.post(alertRunnable);
|
long triggerTime = System.currentTimeMillis() + intervalMs;
|
||||||
|
// Exact alarm ensures it triggers even in Doze mode
|
||||||
return START_STICKY;
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
}
|
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
|
||||||
|
} else {
|
||||||
private void playAlert() {
|
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
|
||||||
// Your existing audio playback code goes here
|
|
||||||
if (mediaPlayer == null) {
|
|
||||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
|
||||||
if (DEBUG) Log.d(tag, "ringtoneUri=" + ringtoneUri.toString());
|
|
||||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
|
||||||
if (mediaPlayer == null) {
|
|
||||||
if (DEBUG) Log.d(tag, "Media Player is null, trying alarm");
|
|
||||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
|
|
||||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
|
||||||
}
|
}
|
||||||
if (mediaPlayer == null) {
|
Log.d(TAG, "Alarm set for " + intervalMs + "ms at " + triggerTime);
|
||||||
if (DEBUG) Log.d(tag, "Media Player is null, trying ringtone");
|
} else {
|
||||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
|
Log.e(TAG, "Could not get AlarmManager service");
|
||||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
|
||||||
}
|
|
||||||
if (mediaPlayer == null) {
|
|
||||||
if (DEBUG) Log.d(tag, "Media Player is null, trying all");
|
|
||||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALL);
|
|
||||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
|
||||||
}
|
|
||||||
if (mediaPlayer == null) {
|
|
||||||
if (DEBUG) Log.d(tag, "Media Player is null, trying local sound byte");
|
|
||||||
mediaPlayer = MediaPlayer.create(this, R.raw.chime_sound_7143);
|
|
||||||
}
|
|
||||||
mediaPlayer.seekTo(0);
|
|
||||||
mediaPlayer.start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public void onDestroy() {
|
* Cancels the timer. If the app is killed, alarms are dropped automatically.
|
||||||
super.onDestroy();
|
*/
|
||||||
handler.removeCallbacks(alertRunnable);
|
public static void stop(Context context) {
|
||||||
if (mediaPlayer != null) {
|
Intent intent = new Intent(context, TimerReceiver.class);
|
||||||
mediaPlayer.release();
|
int flags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT;
|
||||||
mediaPlayer = null;
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
flags |= PendingIntent.FLAG_IMMUTABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, flags);
|
||||||
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
|
||||||
|
if (alarmManager != null) {
|
||||||
|
alarmManager.cancel(pendingIntent);
|
||||||
|
Log.d(TAG, "Alarm cancelled");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public IBinder onBind(Intent intent) {
|
* Checks/Requests permission for exact alarms on Android 12+
|
||||||
return null;
|
*/
|
||||||
|
public static boolean isExactAlarmAllowed(Context context) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
return am != null && am.canScheduleExactAlarms();
|
||||||
|
}
|
||||||
|
return true; // Android 11 and below allow exact alarms by default
|
||||||
}
|
}
|
||||||
|
|
||||||
private Notification buildNotification() {
|
/**
|
||||||
Intent notificationIntent = new Intent(this, MainActivity.class);
|
* Helper to request exact alarm permission if missing
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(
|
*/
|
||||||
this, 0, notificationIntent,
|
public static void requestExactAlarmPermission(android.app.Activity activity) {
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !isExactAlarmAllowed(activity)) {
|
||||||
);
|
Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
|
||||||
|
activity.startActivity(intent);
|
||||||
return new NotificationCompat.Builder(this, CHANNEL_ID)
|
|
||||||
.setContentTitle("Alert Service Running")
|
|
||||||
.setContentText("Playing alerts at " + intervalMs + "ms intervals")
|
|
||||||
.setSmallIcon(R.drawable.ic_notification)
|
|
||||||
.setOngoing(true)
|
|
||||||
.setContentIntent(pendingIntent)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createNotificationChannel() {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
NotificationChannel channel = new NotificationChannel(
|
|
||||||
CHANNEL_ID, "Alert Service", NotificationManager.IMPORTANCE_LOW);
|
|
||||||
getSystemService(NotificationManager.class)
|
|
||||||
.createNotificationChannel(channel);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,13 +5,14 @@ import android.content.DialogInterface;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.media.MediaPlayer;
|
|
||||||
import android.media.Ringtone;
|
import android.media.Ringtone;
|
||||||
import android.media.RingtoneManager;
|
import android.media.RingtoneManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
|
import android.provider.Settings;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -91,48 +92,11 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
if (DEBUG) Log.d(tag, "Got preferences.");
|
if (DEBUG) Log.d(tag, "Got preferences.");
|
||||||
|
|
||||||
// Sound
|
// Sound
|
||||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
// Check exact alarm permission for Android 12+
|
||||||
if (DEBUG) Log.d(tag, "ringtoneUri=" + ringtoneUri.toString());
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !Alert.isExactAlarmAllowed(this)) {
|
||||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
if (DEBUG) Log.w(tag, "Exact alarm permission not granted, requesting...");
|
||||||
if (mediaPlayer == null) {
|
Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
|
||||||
if (DEBUG) Log.d(tag, "Media Player is null, trying alarm");
|
startActivity(intent);
|
||||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
|
|
||||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
|
||||||
}
|
|
||||||
if (mediaPlayer == null) {
|
|
||||||
if (DEBUG) Log.d(tag, "Media Player is null, trying ringtone");
|
|
||||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
|
|
||||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
|
||||||
}
|
|
||||||
if (mediaPlayer == null) {
|
|
||||||
if (DEBUG) Log.d(tag, "Media Player is null, trying all");
|
|
||||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALL);
|
|
||||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
|
||||||
}
|
|
||||||
if (mediaPlayer == null) {
|
|
||||||
if (DEBUG) Log.d(tag, "Media Player is null, trying local sound byte");
|
|
||||||
mediaPlayer = MediaPlayer.create(this, R.raw.chime_sound_7143);
|
|
||||||
}
|
|
||||||
if (mediaPlayer == null) {
|
|
||||||
String message = "Could not find a notification, alarm, or ringtone to play.";
|
|
||||||
message += " App will not be able to function without one of these.";
|
|
||||||
message += " Please exit and install a system notification sound.";
|
|
||||||
|
|
||||||
new AlertDialog.Builder(MainActivity.this)
|
|
||||||
.setTitle("Error")
|
|
||||||
.setMessage(message)
|
|
||||||
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
dialog.dismiss();
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.setCancelable(false)
|
|
||||||
.show()
|
|
||||||
;
|
|
||||||
} else {
|
|
||||||
if (DEBUG) Log.d(tag, "mediaPlayer=" + mediaPlayer.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG){
|
if (DEBUG){
|
||||||
@@ -383,6 +347,20 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
setLoopInterval();
|
setLoopInterval();
|
||||||
resetTimeLeft();
|
resetTimeLeft();
|
||||||
setStartTime();
|
setStartTime();
|
||||||
|
|
||||||
|
// Start the background alert service using Alert manager
|
||||||
|
long interval = longLoopInterval > 1000 ? longLoopInterval : 60000; // Minimum 1 minute interval for alarms
|
||||||
|
if (Alert.isExactAlarmAllowed(this)) {
|
||||||
|
Alert.start(this, interval);
|
||||||
|
} else {
|
||||||
|
if (DEBUG) Log.w(tag, "Cannot start alert: Exact alarm permission denied.");
|
||||||
|
new AlertDialog.Builder(MainActivity.this)
|
||||||
|
.setTitle("Permission Required")
|
||||||
|
.setMessage("Please grant 'All files access' or 'Alarm' permissions in system settings for this feature to work.")
|
||||||
|
.setPositiveButton("OK", (d, w) -> d.dismiss())
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (buttonText.equals(getString(R.string.btnPause))){
|
else if (buttonText.equals(getString(R.string.btnPause))){
|
||||||
btnStartStop.setText(getString(R.string.btnResume));
|
btnStartStop.setText(getString(R.string.btnResume));
|
||||||
@@ -665,6 +643,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
String tag = TAG + "resetAll";
|
String tag = TAG + "resetAll";
|
||||||
if (DEBUG) Log.d(tag, "Starting");
|
if (DEBUG) Log.d(tag, "Starting");
|
||||||
|
|
||||||
|
// Stop the background alert service when resetting
|
||||||
|
Alert.stop(this);
|
||||||
|
|
||||||
if (DEBUG) Log.d(tag, "Resetting screen values...");
|
if (DEBUG) Log.d(tag, "Resetting screen values...");
|
||||||
setChronometer(0);
|
setChronometer(0);
|
||||||
setEditTexts("");
|
setEditTexts("");
|
||||||
@@ -745,8 +726,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
String tag = TAG + "onDestroy";
|
String tag = TAG + "onDestroy";
|
||||||
if (DEBUG) Log.d(tag, "Starting");
|
if (DEBUG) Log.d(tag, "Starting");
|
||||||
|
|
||||||
resetAll();
|
// Stop the background alert service when destroying activity
|
||||||
|
Alert.stop(this);
|
||||||
|
|
||||||
if (DEBUG) Log.d(tag, "Done with local code, calling super..");
|
if (DEBUG) Log.d(tag, "Done with local code, calling super..");
|
||||||
|
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package com.hyperling.apps.infinitetimer;
|
||||||
|
|
||||||
|
import android.app.NotificationChannel;
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.app.Service;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.media.MediaPlayer;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class TimerForegroundService extends Service {
|
||||||
|
private static final String TAG = "TimerFGService";
|
||||||
|
private static final String CHANNEL_ID = "timer_notification_channel";
|
||||||
|
private static final int NOTIFICATION_ID = 1001;
|
||||||
|
|
||||||
|
private PowerManager.WakeLock wakeLock;
|
||||||
|
private MediaPlayer mediaPlayer;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
createNotificationChannel();
|
||||||
|
|
||||||
|
// Android 8+ requires foreground service to play sound reliably while backgrounded
|
||||||
|
startForeground(NOTIFICATION_ID, buildNotification());
|
||||||
|
|
||||||
|
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
|
||||||
|
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TimerWakeLock");
|
||||||
|
// Lock for up to 60 seconds to ensure audio finishes playing even if CPU sleeps
|
||||||
|
wakeLock.acquire(60 * 1000L);
|
||||||
|
|
||||||
|
playSound();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
return START_NOT_STICKY; // Don't restart immediately if killed
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createNotificationChannel() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Timer Notifications", NotificationManager.IMPORTANCE_HIGH);
|
||||||
|
// Disable default sound to avoid double-sound on some OEMs
|
||||||
|
channel.setSound(null, null);
|
||||||
|
channel.enableVibration(false);
|
||||||
|
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||||
|
if (manager != null) manager.createNotificationChannel(channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private android.app.Notification buildNotification() {
|
||||||
|
return new android.app.NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
|
.setContentTitle("Infinite Timer")
|
||||||
|
.setContentText("Timer is active...")
|
||||||
|
// Use system alarm icon temporarily. Add your own ic_stat_notification if desired
|
||||||
|
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
|
||||||
|
.setPriority(android.app.NotificationCompat.PRIORITY_HIGH)
|
||||||
|
.setOngoing(true)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void playSound() {
|
||||||
|
if (mediaPlayer != null && mediaPlayer.isPlaying()) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Plays default notification sound. Replace with R.raw.your_sound if you want custom audio
|
||||||
|
Uri uri = android.media.RingtoneManager.getDefaultUri(android.media.RingtoneManager.TYPE_NOTIFICATION);
|
||||||
|
mediaPlayer = MediaPlayer.create(this, uri);
|
||||||
|
|
||||||
|
if (mediaPlayer != null) {
|
||||||
|
mediaPlayer.setOnCompletionListener(mp -> stopSelf()); // Stop immediately after playing
|
||||||
|
mediaPlayer.start();
|
||||||
|
} else {
|
||||||
|
stopSelf(); // Fallback if media player fails
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Failed to play sound", e);
|
||||||
|
stopSelf();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) { return null; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
if (wakeLock.isHeld()) wakeLock.release();
|
||||||
|
if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.hyperling.apps.infinitetimer;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class TimerReceiver extends BroadcastReceiver {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
if (intent.getAction() != null && Alert.ACTION_TIMER.equals(intent.getAction())) {
|
||||||
|
// Android 8+ requires foreground service to play sound reliably while backgrounded
|
||||||
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||||||
|
context.startForegroundService(new Intent(context, TimerForegroundService.class));
|
||||||
|
} else {
|
||||||
|
context.startService(new Intent(context, TimerForegroundService.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user