我正在尝试在我的应用程序中获取带有计时器的通知。我尝试观看一些指南并从 GPT 获取帮助,但我的服务由于某种原因没有显示我的通知。
这是我的服务,由于某种原因通知不起作用。我做错了什么以及如何解决它?
package com.example.main_app;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;
public class TimerService extends Service {
private static final String TAG = "TimerService";
public static final String TIMER_UPDATED_ACTION = "com.example.main_app.TIMER_UPDATED";
public static final String TIMER_REMAINING_KEY = "timer_remaining";
private static final String CHANNEL_ID = "TimerServiceChannel";
private static final int NOTIFICATION_ID = 1;
private final IBinder binder = new LocalBinder();
private CountDownTimer countDownTimer;
private long timeLeftInMillis;
private boolean timerRunning = false;
public class LocalBinder extends Binder {
TimerService getService() {
return TimerService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "Service onBind");
return binder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service onStartCommand");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (timerRunning) {
countDownTimer.cancel();
}
Log.d(TAG, "Service onDestroy");
}
public void startTimer(long durationInMillis) {
if (timerRunning) {
return;
}
Log.d(TAG, "Timer started for: " + durationInMillis);
timeLeftInMillis = durationInMillis;
countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftInMillis = millisUntilFinished;
Log.d(TAG, "Timer onTick: " + millisUntilFinished);
sendTimerUpdate();
updateNotification(millisUntilFinished);
}
@Override
public void onFinish() {
timerRunning = false;
Log.d(TAG, "Timer finished");
sendTimerUpdate();
updateNotification(0);
stopSelf(); // Stop the service when the timer finishes
}
};
countDownTimer.start();
timerRunning = true;
startForeground(NOTIFICATION_ID, getNotification(timeLeftInMillis));
}
public void pauseTimer() {
if (timerRunning) {
countDownTimer.cancel();
timerRunning = false;
Log.d(TAG, "Timer paused");
sendTimerUpdate();
updateNotification(timeLeftInMillis);
}
}
public void stopTimer() {
if (countDownTimer != null) {
countDownTimer.cancel();
}
timerRunning = false;
timeLeftInMillis = 0;
Log.d(TAG, "Timer stopped");
sendTimerUpdate();
stopForeground(true);
stopSelf(); // Stop the service when the timer is manually stopped
}
private void sendTimerUpdate() {
Intent intent = new Intent(TIMER_UPDATED_ACTION);
intent.putExtra(TIMER_REMAINING_KEY, timeLeftInMillis);
sendBroadcast(intent);
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Timer Service Channel";
String description = "Channel for Timer Service";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
private Notification getNotification(long millisUntilFinished) {
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Timer Running")
.setContentText("Time remaining: " + millisUntilFinished / 1000 + " seconds")
.setSmallIcon(R.drawable.ic_timer)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setOngoing(true)
.build();
}
private void updateNotification(long millisUntilFinished) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.notify(NOTIFICATION_ID, getNotification(millisUntilFinished));
}
}
从这里开始以及以下所有章节:https://developer.android.com/develop/ui/views/notifications 在您的代码中,我看不到您的目标 Android 版本,这会影响您代码的所有架构。特别是为了获得发布通知的权限(https://developer.android.com/develop/ui/views/notifications/notification-permission) 其次,对于来自后台和前台服务的通知有非常严格的规则(https://developer.android.com/develop/background-work/services)。祝你好运,这是一条漫长而曲折的路