我正在試圖製作一個mediaplayer附加到服務的音樂播放器,並且後臺的通知會保持播放狀態。單擊通知欄後活動未恢復
但是,當我點擊通知時,它會創建一個新的活動實例來恢復它。
這裏是我的代碼,請檢查它們: -
@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()沒有爲我工作。
請給我一個這樣做的方法。
任何幫助,將不勝感激...謝謝宇提前:)
嘗試與'android:launchMode =「singleTask」'在活動清單 –
你正在重置媒體播放器在你的'playSong currentSongIndex);'調用。 – tachyonflux
您的服務的唯一目的似乎是設置前臺通知,而您的短暫活動負責實際的播放控制。更好的實現方法是將所有音樂播放實現移到服務中,並提供方法讓您的活動控制它並檢索其狀態。 – Kai