gpt4 book ai didi

java - 从通知导航到 Activity 时丢失 Activity 状态

转载 作者:行者123 更新时间:2023-12-02 09:22:44 25 4
gpt4 key购买 nike

我有一个名为播放器 Activity 的 Activity ,我在其中借助 MediaPlayer API 传输音乐。每当创建我的 Activity 时,都会显示一条通知,其中包含对音乐播放器的一些基本控制。

Notification

因此,当我点击通知时,它会跳回到玩家 Activity ,但 Activity 的状态会丢失。

在点击通知之前:

Before tapping Notification

点击通知后:

After tapping Notification

这是我的通知的待处理 Intent 的代码

Intent notifyIntent = new Intent(context, PlayerActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
notifyIntent.setAction("android.intent.action.MAIN");
notifyIntent.addCategory("android.intent.category.LAUNCHER");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

这是 PlayerActivity.java 的代码:

    package com.example.user.musicplayer;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.concurrent.TimeUnit;

import de.hdodenhof.circleimageview.CircleImageView;

public class PlayerActivity extends AppCompatActivity implements MediaPlayer.OnBufferingUpdateListener,MediaPlayer.OnCompletionListener{

private static Button btn_play_pause;
private Button btnToggleRepeat;
private Button btnStop;
private SeekBar seekBar;
private TextView textView;


public static MediaPlayer mediaPlayer;
private int mediaFileLength;
private int realtimeLength;
private String musicUrl;
private String imageUrl;
final Handler handler = new Handler();
private boolean isRepeat;
private CircleImageView musicImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
Log.d("TAG", "onCreate");
NotificationGenerator.customBigNotification(getApplicationContext());

musicUrl = getIntent().getStringExtra("musicUrl");
imageUrl = getIntent().getStringExtra("imageUrl");
seekBar = (SeekBar)findViewById(R.id.seekbar);
seekBar.setMax(99); // 100% (0~99)
seekBar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(mediaPlayer.isPlaying())
{
SeekBar seekBar = (SeekBar)v;
int playPosition = (mediaFileLength/100)*seekBar.getProgress();
mediaPlayer.seekTo(playPosition);
}
return false;
}
});

textView = (TextView)findViewById(R.id.txtTime);
btnToggleRepeat = findViewById(R.id.btnRepeat);
btnStop = findViewById(R.id.btnStop);
musicImage = findViewById(R.id.musicImgView);

Picasso.get().load(imageUrl).placeholder(R.drawable.music).error(R.drawable.music).into(musicImage);

btn_play_pause = (Button) findViewById(R.id.btnTogglePlay);
btn_play_pause.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

final ProgressDialog mDialog = new ProgressDialog(PlayerActivity.this);


AsyncTask<String,String,String> mp3Play = new AsyncTask<String, String, String>() {

@Override
protected void onPreExecute() {
mDialog.setMessage("Please wait");
mDialog.show();
}

@Override
protected String doInBackground(String... params) {
try{
mediaPlayer.setDataSource(params[0]);
mediaPlayer.prepare();
}
catch (Exception ex)
{
}
return "";
}

@Override
protected void onPostExecute(String s) {
mediaFileLength = mediaPlayer.getDuration();
realtimeLength = mediaFileLength;
if(!mediaPlayer.isPlaying())
{
playMusic();
}
else
{
pauseMusic();
}

updateSeekBar();
mDialog.dismiss();
}
};


mp3Play.execute(musicUrl); // direct link mp3 file

}
});



btnToggleRepeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isRepeat){
isRepeat = false;
mediaPlayer.setLooping(false);
btnToggleRepeat.setText("Repeat");
}
else{
isRepeat = true;
mediaPlayer.setLooping(true);
btnToggleRepeat.setText("Single");

}
}
});

btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mediaPlayer.pause();
mediaPlayer.stop();

}
catch (Exception e){
Toast.makeText(PlayerActivity.this, "Opps! sorry something bad happened", Toast.LENGTH_SHORT).show();
}

}
});


mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);


}

public void pauseMusic() {
mediaPlayer.pause();
btn_play_pause.setText("Play");
}

public void playMusic() {
mediaPlayer.start();
btn_play_pause.setText("Pause");
}


private void updateSeekBar() {
seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / mediaFileLength)*100));
if(mediaPlayer.isPlaying())
{
Runnable updater = new Runnable() {
@Override
public void run() {
updateSeekBar();
realtimeLength-=1000; // declare 1 second
textView.setText(String.format("%d:%d",TimeUnit.MILLISECONDS.toMinutes(realtimeLength),
TimeUnit.MILLISECONDS.toSeconds(realtimeLength) -
TimeUnit.MILLISECONDS.toSeconds(TimeUnit.MILLISECONDS.toMinutes(realtimeLength))));

}

};
handler.postDelayed(updater,1000); // 1 second
}
}

@Override
protected void onResume() {
super.onResume();
Log.d("TAG", "onResume");
}

@Override
protected void onStart() {
super.onStart();
Log.d("TAG", "onStart");
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
seekBar.setSecondaryProgress(percent);
}

@Override
public void onCompletion(MediaPlayer mp) {
if(!mediaPlayer.isLooping())
btn_play_pause.setText("Play");

}

@Override
protected void onStop() {
super.onStop();

}


@Override
protected void onPause() {
super.onPause();
}

public static class DownloadCancelReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

Log.d("notificationPlayer","Received Cancelled Event");
}
}
}

提前致谢。如果解释不清楚,请原谅我,因为如果我有正确的词语来解释它,我会用谷歌搜索它。

最佳答案

将此添加到 list 中的 PlayerActivity Activity 中:

android:launchMode="singleTask"

并在未决 Intent 的 Intent 中使用这些标志:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

关于java - 从通知导航到 Activity 时丢失 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58568969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com