又是我,现在我正在为秒表、闹钟等构建应用程序。所以我在我的代码中添加了服务和通知,但问题是唯一能完美运行的是主视图,或者只是我称之为“TimePlayer.java”,这是我的秒表活动。通知没有出现,我检查了我的代码几次,但不知道我错过了哪一部分。我正在使用目标 API OREO
这是我的服务类:
public class MyServices extends Service implements PropertyChangeListener {
//..........................................................
private static final int NOTIFICATION_ID = 0;
private NotificationManager mNotificationManager;
private boolean isNotificationShowing;
private BroadcastReceiver receiverStateChanged;
private BroadcastReceiver receiverResets;
private BroadcastReceiver receiverExit;
private Timer t;
private Builder mBuilder;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
isNotificationShowing = false;
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
IntentFilter filterNext = new IntentFilter(ACTION_PLAY);
receiverStateChanged = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(TimeContainer.getInstance().getCurrentState() == TimeContainer.STATE_RUNNING) {
TimeContainer.getInstance().pause();
} else {
TimeContainer.getInstance().start();
}
updateNotification();
}
};
registerReceiver(receiverStateChanged, filterNext);
receiverResets = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TimeContainer.getInstance().reset();
updateNotification();
}
};
IntentFilter filterPause = new IntentFilter(ACTION_RESET);
registerReceiver(receiverResets, filterPause);
IntentFilter filterPrev = new IntentFilter(ACTION_STOP);
receiverExit = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TimeContainer.getInstance().reset();
stopForeground(true);
isNotificationShowing = false;
stopSelf();
}
};
registerReceiver(receiverExit, filterPrev);
startUpdateTimer();
TimeContainer.getInstance().isServiceRunning.set(true);
return START_STICKY;
}
@Override
public void onCreate() {
mBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.timer)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setOnlyAlertOnce(true);
super.onCreate();
}
//..........................................................
@SuppressWarnings("deprecation")
private synchronized void updateNotification() {
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_navigation);
if(TimeContainer.getInstance().getCurrentState() == TimeContainer.STATE_RUNNING) {
contentView.setImageViewResource(R.id.btnPlay, R.drawable.ic_launcher_blackpause_foreground);
} else {
contentView.setImageViewResource(R.id.btnPlay, R.drawable.ic_launcher_blackplay_foreground);
}
contentView.setTextViewText(R.id.textNotifTime, msToHourMinSec(TimeContainer.getInstance().getElapsedTime()) );
//..........................................................
mBuilder.setContent(contentView);
Intent notificationIntent = new Intent(this, TimePlayer.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, PendingIntent.FLAG_UPDATE_CURRENT, notificationIntent, 0);
mBuilder.setContentIntent(intent);
if(isNotificationShowing) {
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.getNotification());
} else {
isNotificationShowing = true;
startForeground(NOTIFICATION_ID, mBuilder.getNotification());
}
}
//..........................................................
}
如果您有任何解决方案,请帮助我。无论如何,提前感谢