繁体   English   中英

Android:从具有remoteview的多个通知更新特定通知

[英]Android: update specific notification from multiple notifications which have remoteview

我正在创建包含播放/暂停图标的通知的应用程序。 当只有一个通知只有一个通知ID时它完美地工作,它只通过一个notificationid改变播放/暂停图标,但是存在多个通知的问题。 当有多个通知时,每个通知都有播放/暂停图标。 当我从其中一个通知中单击播放/暂停图标时,它只会从通知堆栈中更改顶部通知图标。 我知道这个问题是由于它点击任何通知所得到的通知ID。我想获得点击播放/暂停图标的所有通知的ID。我尝试了一些pendingIntent内容的更改,但还没有找到确切的解决方案。

我想知道, 有没有办法获得通知的通知,点击该播放/暂停图标? ,所以我可以通过通知ID更改图标或更新该通知。需要通过通知ID更新通知。

我已经使用远程视图来获取通知中的服装布局。 代码如下

NotificationCode

    RemoteViews mRemoteViews1 = null;
    PendingIntent pendingIntent = null;
    PendingIntent pendingIntent1 = null;
    PendingIntent pendingIntent2 = null;
    PendingIntent pendingIntent3 = null;
    PendingIntent pendingIntent4 = null;
    PendingIntent pendingIntent5 = null;

    if (mRemoteViews1 == null) {
        Log.d("LOG", "mRemoteViews is null");

            mRemoteViews1 = new RemoteViews(getPackageName(),
                    R.layout.custom_notification);

    } else {
        if (mState == palyer.Paused || mState == palyer.Stopped) {

            try {
                Log.e("LOG", "State.Paused || State.Stopped");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.playdetailfornoti);  // play icon

            } catch (Exception e) {}

        } else if (mState == palyer.Playing) {
            try {
                Log.e("LOG", "State.Playing");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.pushdetail);   // pushicon

            } catch (Exception e) {}
        } else if (mState == palyer.Retrieving) {
            try {
                Log.e("LOG", "else Retrieving");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.playdetailfornoti); // play icon

            } catch (Exception e) {}
        }
        else {
            try {
                Log.e("LOG", "else");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.pushdetail);   // pushicon

            } catch (Exception e) {}
        }
    }

    Intent intent = new Intent(ACTION_TOGGLE_PLAYBACK);
    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);

    Intent newintent = new Intent(this, Splace.class);
    newintent.putExtra("newsId",_id);
    newintent.putExtra("message",title);
    newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    newintent.setAction("actionstring" + System.currentTimeMillis());

    pendingIntent = PendingIntent.getService(getApplicationContext(),
            REQUEST_CODE_STOP, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent1 = PendingIntent.getService(getApplicationContext(), 0,
            intent1, 0);
    pendingIntent2 = PendingIntent.getService(getApplicationContext(),
            REQUEST_CODE_STOP, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent3 = PendingIntent.getService(getApplicationContext(),
            REQUEST_CODE_STOP, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            newintent, 0);
    pendingIntent4 = PendingIntent.getService(getApplicationContext(),
            REQUEST_CODE_STOP, intent4, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent5 = PendingIntent.getService(getApplicationContext(),
            REQUEST_CODE_STOP, intent5, PendingIntent.FLAG_UPDATE_CURRENT);


    mRemoteViews1.setTextViewText(R.id.playertitle,
            MusicList.CurrentNotificationEntity.scrape_title);

    mRemoteViews1
                .setTextViewText(R.id.playerapaer, Base
                        .getNewsPaperName(Base.sharedPref.getString(
                                MusicList.CurrentNotificationEntity.newspaperID,
                                "Palpal")));
    mRemoteViews1.setOnClickPendingIntent(R.id.forward, pendingIntent2);
    mRemoteViews1.setOnClickPendingIntent(R.id.backword, pendingIntent3);

    mRemoteViews1.setOnClickPendingIntent(R.id.playertitle, contentIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.playerapaer, contentIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.playerimage, contentIntent);

    mRemoteViews1.setOnClickPendingIntent(R.id.playpush, pendingIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.close, pendingIntent1);

    Notification  mNotification1 = new NotificationCompat.Builder(
                getApplicationContext())
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_HIGH)
                .setDeleteIntent(pendingIntent1)
                .setAutoCancel(true)
                .setContent(mRemoteViews1).setOngoing(false).build();

    mNotificationManager.notify(Base.currentNotificationNo, mNotification1);

请提前帮助我

我只想在您放入PendingIntent的意图中添加一个ID,以了解您正在操作的通知,如下所示:

Intent intent = new Intent(ACTION_TOGGLE_PLAYBACK);

intent.putExtra(EXTRA_ID,songID);

您可以稍后在调用的代码中提取此额外内容:

字符串songID = intent.getStringExtra(EXTRA_ID);

经过大量尝试后,我通过更改待处理意图中的一些代码来找出解决方案,如下面的代码所示

我的代码如下

public void UpdateNotificationfunction(int currentNotificationNo) {
 RemoteViews mRemoteViews1 = null;
    PendingIntent pendingIntent = null;
    PendingIntent pendingIntent1 = null;
    PendingIntent pendingIntent2 = null;
    PendingIntent pendingIntent3 = null;
    PendingIntent pendingIntent4 = null;
    PendingIntent pendingIntent5 = null;

    if (mRemoteViews1 == null) {
        Log.d("LOG", "mRemoteViews is null");

            mRemoteViews1 = new RemoteViews(getPackageName(),
                    R.layout.custom_notification);

    } else { 
        if (mState == palyer.Paused || mState == palyer.Stopped) { 

            try { 
                Log.e("LOG", "State.Paused || State.Stopped");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.playdetailfornoti);  // play icon

            } catch (Exception e) {}

        } else if (mState == palyer.Playing) { 
            try { 
                Log.e("LOG", "State.Playing");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.pushdetail);   // pushicon

            } catch (Exception e) {}
        } else if (mState == palyer.Retrieving) { 
            try { 
                Log.e("LOG", "else Retrieving");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.playdetailfornoti); // play icon

            } catch (Exception e) {}
        } 
        else { 
            try { 
                Log.e("LOG", "else");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.pushdetail);   // pushicon

            } catch (Exception e) {}
        } 
    } 

    Intent intent = new Intent(ACTION_TOGGLE_PLAYBACK);
    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);

    Intent newintent = new Intent(this, Splace.class);
    newintent.putExtra("newsId",_id);
    newintent.putExtra("message",title);
    newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    newintent.setAction("actionstring" + System.currentTimeMillis());

    pendingIntent = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent, PendingIntent.FLAG_UPDATE_CURRENT); // changed here currentNotificationNo insted of REQUEST_CODE_STOP
    pendingIntent1 = PendingIntent.getService(getApplicationContext(), 0,
            intent1, 0);
    pendingIntent2 = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent2, PendingIntent.FLAG_UPDATE_CURRENT);// changed here currentNotificationNo insted of REQUEST_CODE_STOP
    pendingIntent3 = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent3, PendingIntent.FLAG_UPDATE_CURRENT);// changed here currentNotificationNo insted of REQUEST_CODE_STOP
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            newintent, 0);
    pendingIntent4 = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent4, PendingIntent.FLAG_UPDATE_CURRENT);// changed here currentNotificationNo insted of REQUEST_CODE_STOP
    pendingIntent5 = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent5, PendingIntent.FLAG_UPDATE_CURRENT);// changed here currentNotificationNo insted of REQUEST_CODE_STOP


    mRemoteViews1.setTextViewText(R.id.playertitle,
            MusicList.CurrentNotificationEntity.scrape_title); 

    mRemoteViews1
                .setTextViewText(R.id.playerapaer, Base
                        .getNewsPaperName(Base.sharedPref.getString( 
                                MusicList.CurrentNotificationEntity.newspaperID, 
                                "Palpal"))); 
    mRemoteViews1.setOnClickPendingIntent(R.id.forward, pendingIntent2);
    mRemoteViews1.setOnClickPendingIntent(R.id.backword, pendingIntent3);

    mRemoteViews1.setOnClickPendingIntent(R.id.playertitle, contentIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.playerapaer, contentIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.playerimage, contentIntent);

    mRemoteViews1.setOnClickPendingIntent(R.id.playpush, pendingIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.close, pendingIntent1);

    Notification  mNotification1 = new NotificationCompat.Builder(
                getApplicationContext())
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_HIGH)
                .setDeleteIntent(pendingIntent1)
                .setAutoCancel(true) 
                .setContent(mRemoteViews1).setOngoing(false).build();

    mNotificationManager.notify(currentNotificationNo, mNotification1); // changed here currentNotificationNo insted of Base.currentNotificationNo
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM