alert, begin adding changes to have a foreground service.

This commit is contained in:
me
2026-10-02 06:54:52 -07:00
parent 4e474f3fce
commit 72a77eeef2
2 changed files with 133 additions and 0 deletions
+8
View File
@@ -14,6 +14,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<service
android:name=".Alert"
android:foregroundServiceType="mediaPlayback"
android:exported="false" />
</application>
</manifest>
@@ -0,0 +1,125 @@
package com.hyperling.apps.infinitetimer;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class Alert extends Service {
boolean DEBUG = true;
String TAG = "Alert.", tag;
private static final String CHANNEL_ID = "alert_service_channel";
private static final int NOTIFICATION_ID = 1;
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();
startForeground(NOTIFICATION_ID, buildNotification());
// Start the alert loop (your original Runnable logic)
handler.post(alertRunnable);
return START_STICKY;
}
private void playAlert() {
// 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) {
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);
}
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(alertRunnable);
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private Notification buildNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
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);
}
}
}