ساعت سخنگو برای فارسی زبانان
| import android.content.Intent; |
| 9 | import android.content.SharedPreferences; |
| 10 | import android.media.MediaPlayer; |
| 11 | import android.net.Uri; |
| 12 | import android.os.Build; |
| 13 | import android.os.Handler; |
| 14 | import android.os.IBinder; |
| 15 |
| 16 | public class ReminderService extends Service { |
| 17 | private MediaPlayer player; |
| 18 | private static final String CH = "reminders"; |
| 19 | private static final String EXTRA_SLOT = "reminder_slot"; |
| 20 |
| 21 | public static void start(Context c, int slot) { |
| 22 | Intent i = new Intent(c, ReminderService.class).putExtra(EXTRA_SLOT, slot); |
| 23 | if (Build.VERSION.SDK_INT >= 26) c.startForegroundService(i); |
| 24 | else c.startService(i); |
| 25 | } |
| 26 |
| 27 | public static void start(Context c) { |
| 28 | start(c, 1); |
| 29 | } |
| 30 |
| 31 | @Override public void onCreate() { |
| 32 | super.onCreate(); |
| 33 | if (Build.VERSION.SDK_INT >= 26) { |
| 34 | NotificationChannel ch = new NotificationChannel( |
| 35 | CH, "یادآوریها", NotificationManager.IMPORTANCE_HIGH); |
| 36 | NotificationManager nm = getSystemService(NotificationManager.class); |
| 37 | if (nm != null) nm.createNotificationChannel(ch); |
| 38 | } |
| 39 | } |
| 40 |
| 41 | @Override public int onStartCommand(Intent intent, int flags, int startId) { |
| 42 | int slot = intent != null ? intent.getIntExtra(EXTRA_SLOT, 1) : 1; |
| 43 | if (slot < 1 || slot > 5) slot = 1; |
| 44 |
| 45 | SharedPreferences p = getSharedPreferences("settings", MODE_PRIVATE); |
| 46 | String prefix = "reminder" + slot; |
| 47 | String text = p.getString(prefix + "Text", "یادآوری"); |
| 48 | String audio = p.getString(prefix + "AudioUri", ""); |
| 49 |
| 50 | android.widget.RemoteViews rv = new android.widget.RemoteViews(getPackageName(), R.layout.notification_large); |
| 51 | rv.setTextViewText(R.id.notificationTitle, "یادآوری " + slot); |
| 52 | rv.setTextViewText(R.id.notificationText, text); |
| 53 | Notification n; |
| 54 | if (Build.VERSION.SDK_INT >= 26) { |
| 55 | n = new Notification.Builder(this, CH) |
| 56 | .setSmallIcon(R.drawable.ic_status_clock) |
| 57 | .setCustomContentView(rv) |
| 58 | .setContentTitle("یادآوری " + slot) |
| 59 | .setContentText(text) |
| 60 | .setAutoCancel(true) |
| 61 | .setOngoing(false) |
| 62 | .build(); |
| 63 | } else { |
| 64 | n = new Notification.Builder(this) |
| 65 | .setSmallIcon(R.drawable.ic_status_clock) |
| 66 | .setContent(rv) |
| 67 | .setContentTitle("یادآوری " + slot) |
| 68 | .setContentText(text) |
| 69 | .setAutoCancel(true) |
| 70 | .setOngoing(false) |
| 71 | .build(); |
| 72 | } |
| 73 | startForeground(1900 + slot, n); |
| 74 | play(audio); |
| 75 | new Handler().postDelayed(this::stopSelf, 15000); |
| 76 | return START_NOT_STICKY; |
| 77 | } |
| 78 |
| 79 | private void play(String uri) { |
| 80 | try { |
| 81 | if (uri != null && !uri.isEmpty()) player = MediaPlayer.create(this, Uri.parse(uri)); |
| 82 | if (player == null) { |
| 83 | int resId = getResources().getIdentifier("reminder_chime", "raw", getPackageName()); |
| 84 | if (resId != 0) player = MediaPlayer.create(this, resId); |
| 85 | } |
| 86 | if (player != null) { |
| 87 | player.setOnCompletionListener(mp -> { |
| 88 | mp.release(); |
| 89 | if (player == mp) player = null; |
| 90 | }); |
| 91 | player.start(); |
| 92 | } |
| 93 | } catch (Exception ignored) { |
| 94 | if (player != null) { |
| 95 | try { player.release(); } catch (Exception ignored2) {} |
| 96 | player = null; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 |
| 101 | @Override public void onDestroy() { |
| 102 | if (player != null) { |
| 103 | try { if (player.isPlaying()) player.stop(); } catch (Exception ignored) {} |
| 104 | try { player.release(); } catch (Exception ignored) {} |
| 105 | player = null; |
| 106 | } |
| 107 | super.onDestroy(); |
| 108 | } |
| 109 |
| 110 | @Override public IBinder onBind(Intent intent) { return null; } |
| 111 | } |