Android 多个通知避免更新时闪烁
Android multiple notification avoid blinking while updating
我正在开发这样的应用程序,它使用多个 notification.where 每个通知都有播放暂停图标,可以执行 play/pause 音乐播放器。但是更新通知时会出现奇怪的行为,该通知会在更新时不断闪烁。
我的代码在下面
@SuppressLint("NewApi")
public void UpdateNotificationfunction(int currentNotificationNo) {
PendingIntent pendingIntent = null;
PendingIntent pendingIntent1 = null;
PendingIntent pendingIntent2 = null;
PendingIntent pendingIntent3 = null;
PendingIntent pendingIntent4 = null;
PendingIntent pendingIntent5 = null;
if (mRemoteViews1 == null || newRemoteview) {
Log.d("LOG", "mRemoteViews is null 123");
mRemoteViews1 = new RemoteViews(getPackageName(),
R.layout.custom_notification);
newRemoteview = false;
} else {
if (mState == State.Paused || mState == State.Stopped) {
try {
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.playdetailfornoti);
} catch (Exception e) {
}
} else if (mState == State.Playing) {
try {
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.pushdetail);
} catch (Exception e) {
}
} else if (mState == State.Retrieving) {
try {
Log.e("LOG", "else Retrieving");
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.playdetailfornoti);
} catch (Exception e) {
}
} else {
try {
Log.e("LOG", "else");
if (NetworkUtil.isNetworkAvailable(_context)) {
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.pushdetail);
}
else {
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.playdetailfornoti);
}
} catch (Exception e) {
}
}
}
Intent intent = new Intent(ACTION_TOGGLE_PLAYBACK);
intent.removeExtra("playposition");
intent.putExtra("playposition", currentNotificationNo);
Intent intent1 = new Intent(CLOSE_PUSH_NOTIFICATION);
Intent intent2 = new Intent(ACTION_NEXT);
Intent intent3 = new Intent(ACTION_PREVIOUS);
Intent intent4 = new Intent(ACTION_STOP);
Intent intent5 = new Intent(ACTION_PLAY_NOTIFICATION_START);
intent5.removeExtra("playposition");
intent1.removeExtra("playposition"); // here i remove put extra
intent5.putExtra("playposition", currentNotificationNo);
intent1.putExtra("playposition", currentNotificationNo);// here i add put extra with current notification id
Log.e("LOG", "poistionset || " + currentNotificationNo);
Intent newintent = new Intent(this, Splace.class);
newintent.putExtra("newsId",MusicList.CurrentNotificationEntity._id);
newintent.putExtra("playposition", currentNotificationNo);
pendingIntent = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent, PendingIntent.FLAG_ONE_SHOT);
pendingIntent1 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent2 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent3 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent3,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), currentNotificationNo, newintent,
PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent4 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent4,
PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent5 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent5,
PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews1.setTextViewText(R.id.playertitle,
MusicList.CurrentNotificationEntity.scrape_title);
mRemoteViews1.setViewVisibility(R.id.playernewspapaer, View.VISIBLE);
mRemoteViews1.setViewVisibility(R.id.forward, View.VISIBLE);
mRemoteViews1.setViewVisibility(R.id.backword, View.VISIBLE);
mRemoteViews1.setOnClickPendingIntent(R.id.forward, pendingIntent2);
mRemoteViews1.setOnClickPendingIntent(R.id.backword, pendingIntent3);
mRemoteViews1.setOnClickPendingIntent(R.id.playertitle, contentIntent);
mRemoteViews1.setOnClickPendingIntent(R.id.playernewspapaer,
contentIntent);
mRemoteViews1.setOnClickPendingIntent(R.id.playerimage, contentIntent);
//if (mNotification1 == null) {
mNotification1 = new NotificationCompat2.Builder(
getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_DEFAULT)
.setDeleteIntent(pendingIntent1).setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContent(mRemoteViews1).setOngoing(false).build();
/*}
else {
mNotification1.contentView = mRemoteViews1;
}*/ //this comment solve my same id issue , I have put this for avoid blinking of notification
.setContent(mRemoteViews1).setOngoing(false).build();
mNotificationManager.notify(currentNotificationNo, mNotification1);
}
请帮助我
您提供一个 PendingIntent
,当用户删除通知时将发送该通知。这个 PendingIntent
是在构造通知时调用 setDeleteIntent()
时提供的。当您的通知被用户删除时,您无法 Android 发送 5 PendingIntent
秒。
经过多次尝试,我找到了闪烁问题和重复 deleteintent 问题的解决方案
if (mNotification1 == null) {
// create when notification not avalible
mNotification1 = new NotificationCompat2.Builder(
getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_DEFAULT)
.setDeleteIntent(pendingIntent1).setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContent(mRemoteViews1).setOngoing(false).build();
}
else {
// this code will solve the blink issue
mNotification1.deleteIntent = pendingIntent1; // this will solve the deleteintent issue
mNotification1.contentView = mRemoteViews1;
}
我正在开发这样的应用程序,它使用多个 notification.where 每个通知都有播放暂停图标,可以执行 play/pause 音乐播放器。但是更新通知时会出现奇怪的行为,该通知会在更新时不断闪烁。
我的代码在下面
@SuppressLint("NewApi")
public void UpdateNotificationfunction(int currentNotificationNo) {
PendingIntent pendingIntent = null;
PendingIntent pendingIntent1 = null;
PendingIntent pendingIntent2 = null;
PendingIntent pendingIntent3 = null;
PendingIntent pendingIntent4 = null;
PendingIntent pendingIntent5 = null;
if (mRemoteViews1 == null || newRemoteview) {
Log.d("LOG", "mRemoteViews is null 123");
mRemoteViews1 = new RemoteViews(getPackageName(),
R.layout.custom_notification);
newRemoteview = false;
} else {
if (mState == State.Paused || mState == State.Stopped) {
try {
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.playdetailfornoti);
} catch (Exception e) {
}
} else if (mState == State.Playing) {
try {
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.pushdetail);
} catch (Exception e) {
}
} else if (mState == State.Retrieving) {
try {
Log.e("LOG", "else Retrieving");
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.playdetailfornoti);
} catch (Exception e) {
}
} else {
try {
Log.e("LOG", "else");
if (NetworkUtil.isNetworkAvailable(_context)) {
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.pushdetail);
}
else {
mRemoteViews1.setImageViewResource(R.id.playpush,
R.drawable.playdetailfornoti);
}
} catch (Exception e) {
}
}
}
Intent intent = new Intent(ACTION_TOGGLE_PLAYBACK);
intent.removeExtra("playposition");
intent.putExtra("playposition", currentNotificationNo);
Intent intent1 = new Intent(CLOSE_PUSH_NOTIFICATION);
Intent intent2 = new Intent(ACTION_NEXT);
Intent intent3 = new Intent(ACTION_PREVIOUS);
Intent intent4 = new Intent(ACTION_STOP);
Intent intent5 = new Intent(ACTION_PLAY_NOTIFICATION_START);
intent5.removeExtra("playposition");
intent1.removeExtra("playposition"); // here i remove put extra
intent5.putExtra("playposition", currentNotificationNo);
intent1.putExtra("playposition", currentNotificationNo);// here i add put extra with current notification id
Log.e("LOG", "poistionset || " + currentNotificationNo);
Intent newintent = new Intent(this, Splace.class);
newintent.putExtra("newsId",MusicList.CurrentNotificationEntity._id);
newintent.putExtra("playposition", currentNotificationNo);
pendingIntent = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent, PendingIntent.FLAG_ONE_SHOT);
pendingIntent1 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent2 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent3 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent3,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), currentNotificationNo, newintent,
PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent4 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent4,
PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent5 = PendingIntent.getService(getApplicationContext(),
currentNotificationNo, intent5,
PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews1.setTextViewText(R.id.playertitle,
MusicList.CurrentNotificationEntity.scrape_title);
mRemoteViews1.setViewVisibility(R.id.playernewspapaer, View.VISIBLE);
mRemoteViews1.setViewVisibility(R.id.forward, View.VISIBLE);
mRemoteViews1.setViewVisibility(R.id.backword, View.VISIBLE);
mRemoteViews1.setOnClickPendingIntent(R.id.forward, pendingIntent2);
mRemoteViews1.setOnClickPendingIntent(R.id.backword, pendingIntent3);
mRemoteViews1.setOnClickPendingIntent(R.id.playertitle, contentIntent);
mRemoteViews1.setOnClickPendingIntent(R.id.playernewspapaer,
contentIntent);
mRemoteViews1.setOnClickPendingIntent(R.id.playerimage, contentIntent);
//if (mNotification1 == null) {
mNotification1 = new NotificationCompat2.Builder(
getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_DEFAULT)
.setDeleteIntent(pendingIntent1).setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContent(mRemoteViews1).setOngoing(false).build();
/*}
else {
mNotification1.contentView = mRemoteViews1;
}*/ //this comment solve my same id issue , I have put this for avoid blinking of notification
.setContent(mRemoteViews1).setOngoing(false).build();
mNotificationManager.notify(currentNotificationNo, mNotification1);
}
请帮助我
您提供一个 PendingIntent
,当用户删除通知时将发送该通知。这个 PendingIntent
是在构造通知时调用 setDeleteIntent()
时提供的。当您的通知被用户删除时,您无法 Android 发送 5 PendingIntent
秒。
经过多次尝试,我找到了闪烁问题和重复 deleteintent 问题的解决方案
if (mNotification1 == null) {
// create when notification not avalible
mNotification1 = new NotificationCompat2.Builder(
getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_DEFAULT)
.setDeleteIntent(pendingIntent1).setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContent(mRemoteViews1).setOngoing(false).build();
}
else {
// this code will solve the blink issue
mNotification1.deleteIntent = pendingIntent1; // this will solve the deleteintent issue
mNotification1.contentView = mRemoteViews1;
}