Android多个通知,避免更新时闪烁

Android多个通知,避免更新时闪烁,第1张

概述我正在使用多个通知的此类应用程序.每个通知都有播放暂停图标,用于执行音乐播放器的播放/暂停.但是更新通知的行为很奇怪,通知在更新时一直闪烁.我的代码如下@SuppressLint("NewApi")publicvoidUpdateNotificationfunction(intcurrentNotificationNo){Pendin

我正在使用多个通知的此类应用程序.每个通知都有播放暂停图标,用于执行音乐播放器的播放/暂停.但是更新通知的行为很奇怪,通知在更新时一直闪烁.

我的代码如下

 @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);    }

请帮我

解决方法:

尝试了很多之后,我找到了闪烁问题和重复的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;    }
总结

以上是内存溢出为你收集整理的Android多个通知,避免更新时闪烁全部内容,希望文章能够帮你解决Android多个通知,避免更新时闪烁所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1094584.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存