Android 通知未显示 Java 秒表
Android Notification is not Showing Java for Stop Watch
又是我,现在我正在为秒表、闹钟等构建一个应用程序。
所以我在我的代码中添加了服务和通知,但问题是唯一运行完美的是主视图,或者我简单地称之为“TimePlayer.java”,这个是我的秒表 activity。通知没有出现,我检查了我的代码好几次,但不知道我错过了哪一部分。我正在使用目标 API OREO
这是我的服务 Class :
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());
}
}
//..........................................................
}
如果您有任何解决方案,请帮助我。无论如何,提前谢谢
终于在4个小时后,解决了自己的问题。事情是这样的,我的通知已完全弃用(XD 抱歉)。我以前甚至没有注意到这一点。我的应用程序在 targetSdk 26 和 minSdk 23 上是 运行,这意味着我上面的代码将无法工作,因为大多数功能已被弃用,这就是为什么我必须为 minSdk 添加一些抑制。最后但同样重要的是,最后,我必须添加 Notification Compat 和 Notification Channel 来调用通知。这是Builder本身造成的。我在上面使用了带有 Builder 的自变量,它应该调用 Notification Compat。
到目前为止,这是我所做的更改:
private NotificationCompat.Builder mBuilder;
//..................
NotificationChannel mChannel = new NotificationChannel(notifyID, "name", importance);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.createNotificationChannel(mChannel);
}
//..................
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//..................
mBuilder.setSmallIcon(R.drawable.timer);
mBuilder.setContentIntent(intent);
mBuilder.setChannelId(notifyID);
mBuilder.build();
}
又是我,现在我正在为秒表、闹钟等构建一个应用程序。 所以我在我的代码中添加了服务和通知,但问题是唯一运行完美的是主视图,或者我简单地称之为“TimePlayer.java”,这个是我的秒表 activity。通知没有出现,我检查了我的代码好几次,但不知道我错过了哪一部分。我正在使用目标 API OREO
这是我的服务 Class :
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());
}
}
//..........................................................
}
如果您有任何解决方案,请帮助我。无论如何,提前谢谢
终于在4个小时后,解决了自己的问题。事情是这样的,我的通知已完全弃用(XD 抱歉)。我以前甚至没有注意到这一点。我的应用程序在 targetSdk 26 和 minSdk 23 上是 运行,这意味着我上面的代码将无法工作,因为大多数功能已被弃用,这就是为什么我必须为 minSdk 添加一些抑制。最后但同样重要的是,最后,我必须添加 Notification Compat 和 Notification Channel 来调用通知。这是Builder本身造成的。我在上面使用了带有 Builder 的自变量,它应该调用 Notification Compat。
到目前为止,这是我所做的更改:
private NotificationCompat.Builder mBuilder;
//..................
NotificationChannel mChannel = new NotificationChannel(notifyID, "name", importance);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.createNotificationChannel(mChannel);
}
//..................
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//..................
mBuilder.setSmallIcon(R.drawable.timer);
mBuilder.setContentIntent(intent);
mBuilder.setChannelId(notifyID);
mBuilder.build();
}