使用BroadCast Reciever

时间:2015-08-17 14:51:37

标签: android notifications

我收到通知,我正在尝试更新通知,但我的BroadCast接收器出现问题。

我正在调用我的方法,在我的广播接收器中更新通知:

        this.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                nextSongorPrevious(songPathsAll, "next", true);

                updateNotification("");

            }
        }, new IntentFilter("Next"));

具有意图Next的广播接收器连接到这样的按钮:

 PendingIntent next = PendingIntent.getBroadcast(this, 1,
                new Intent("Next"), 0);
        contentView.setOnClickPendingIntent(R.id.nNext, next);

这是我打电话来更新我的通知的方法:

  public void updateNotification(String what) {

    Notification notification = getMyActivityNotification(what);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notif_id, notification);
}

我遇到的问题是当我调用方法来更新我的通知时,"下一步"广播接收器被一遍又一遍地调用,我不知道为什么。

以下是完整的通知:

private Notification getMyActivityNotification(String text){
    // The PendingIntent to launch our activity if the user selects
    // this notification

    PendingIntent contentIntent = PendingIntent.getActivity(this,
            0, new Intent(this, Main_activity.class), 0);

    Notification.Builder mainBuilder = new Notification.Builder(this);



    RemoteViews contentView = new RemoteViews(
            context.getPackageName(), R.layout.notification_layout);


    contentView.setTextViewText(R.id.nSongArtist,
            ArtistNames.get(position2));

    contentView.setTextViewText(R.id.nSongTitle,
            SongName());





    if(text == "play"){

        contentView.setImageViewResource(R.id.nPlay,
                R.drawable.pause_black);


    }

    if(text == "pause"){

        contentView.setImageViewResource(R.id.nPlay,
                R.drawable.play_black);



    }

    if(text == "pause"){

        contentView.setImageViewResource(R.id.nPlay,
                R.drawable.play_black);


    }


        this.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                nextSongorPrevious(songPathsAll, "next", true);

                Intent i = new Intent("fsNext");
                sendBroadcast(i);

                updateNotification("");


            }
        }, new IntentFilter("Next"));



        PendingIntent next = PendingIntent.getBroadcast(this, 1,
                new Intent("Next"), 0);

        contentView.setOnClickPendingIntent(R.id.nNext, next);



    return mainBuilder
                .setContent(contentView)

            .setSmallIcon(R.drawable.play_white)

            .setContentTitle(SongName())
            .setContentText(ArtistNames.get(position2))
                .setContentIntent(contentIntent).getNotification();



}

如果有人可以帮助我,那将非常感激。

0 个答案:

没有答案