单击通知栏后活动未恢复

时间:2015-10-18 13:50:18

标签: android android-layout service android-mediaplayer remoteview

我正在尝试创建一个音乐播放器,其中媒体播放器连接到服务,背景中的通知保持播放。

但是当我点击通知时,它会创建一个新的活动实例而不是恢复它。

以下是我的代码,请检查一下: -

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.player);


     MusicService.mPlayer = new MediaPlayer();
        MusicService.mPlayer.setOnCompletionListener(this); // Important


startService(Explicit(getApplicationContext(),new Intent(MusicService.ACTION_SETUP)));
playSong(currentSongIndex);

}
public static void  playSong(int songIndex){
            MusicService.mPlayer.reset(); 
                    MusicService.mPlayer.setDataSource(getPathForsongname(Playlist.get(songIndex)));
                     MusicService.mPlayer.prepare();
                     MusicService.mPlayer.start();


                songTitle = Playlist.get(songIndex);
                songTitleLabel.setText(songTitle);
                songArtistLabel.setText(getArtistForsongname(songTitle));
                songProgressBar.setProgress(0);
                songProgressBar.setMax(100);

                updateProgressBar();//It updates the progress by using handler progress bar simply


            } 

        }

现在正在服务中我正在设置这样的前景: -

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
         if (action.equals(ACTION_SETUP)){
        setupAsForeground(AndroidBuildingMusicPlayerActivity.Playlist.get(AndroidBuildingMusicPlayerActivity.currentSongIndex));
        }
    return START_NOT_STICKY;   }

void setUpAsForeground(String text) {

        stopForeground(true);
        Intent intent = new Intent(this, AndroidBuildingMusicPlayerActivity.class);

        PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
  RemoteViews r = new RemoteViews(this.getPackageName(),
                R.layout.notification_layout);//set your custom layout
        Intent nextIntent = new Intent(this, MusicService.class);
        nextIntent.setAction(ACTION_SKIP);
        PendingIntent pnextIntent = PendingIntent.getService(this, 0,
                 nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Intent prevIntent = new Intent(this, MusicService.class);
        prevIntent.setAction(ACTION_PREVIOUS);

        PendingIntent pprevIntent = PendingIntent.getService(this, 0,
                 prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Intent playIntent = new Intent(this, MusicService.class);
        playIntent.setAction(ACTION_PLAY);
        PendingIntent pplayIntent = PendingIntent.getService(this, 0,
                 playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mSongTitle=AndroidBuildingMusicPlayerActivity.Playlist.get(AndroidBuildingMusicPlayerActivity.current);
        artist=AndroidBuildingMusicPlayerActivity.getArtistForsongname(mSongTitle);
        r.setOnClickPendingIntent(R.id.btnNext, pnextIntent); 
        r.setOnClickPendingIntent(R.id.btnPrevious, pprevIntent); 
        r.setOnClickPendingIntent(R.id.btnPlay, pplayIntent); 
        r.setTextViewText(R.id.songTitle1, mSongTitle);
        r.setTextViewText(R.id.Artist1, artist);
        r.setFloat(R.id.songTitle1, "setTextSize", 14);
        Notification.Builder mBuilder = new Notification.Builder(  
                this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(" Music Player")
        .setContentText(text)

        .setContent(r)
        .setContentIntent(pi)
                .setOngoing(true)
                  .setWhen(0)

                .setAutoCancel(false);

      startForeground(1,mBuilder.build());
    }

但是当我通过从最近的任务中删除活动来销毁活动时。

然后点击通知栏,它会启动一项新活动并从0:00开始播放歌曲。

无论如何都要从同一个州重启活动? 我试过onsaveinstance()和onrestoreinstance()对我没用。

请建议我这样做。

任何帮助将不胜感激...提前感谢yu :)

2 个答案:

答案 0 :(得分:3)

使用以下代码创建待处理的意图:

PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

答案 1 :(得分:1)

您可以在服务中移动play()功能。当你想要播放一首歌时,只需要开始使用需要属性的服务,例如你的歌曲uri等。