这是我的代码:
package com.justforyou.bestnarutosongs;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
public class SongsListActivity extends AppCompatActivity
{
public boolean IsPaused = false;
public int Old_Song = -326523;
public TextView NameD;
public ImageView ImageD;
public TextView RateD;
public int II;
public Runnable UpdateSongTime;
public int startTime;
public int IIII;
public Handler myHandler = new Handler();
public SeekBar seekBar;
public TextView Current_Time;
public TextView Duration;
public int Repeat_Value = 0;
public ImageButton Stop;
public Songs song;
public ArrayList<Songs> Song;
public ImageButton P_and_P = null;
public ImageButton Repeat = null;
public ImageButton Next = null;
public ImageButton Previous = null;
private MediaPlayer mediaplayer;
private AudioManager mAudioManager;
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener()
{
@Override
public void onAudioFocusChange(int focusChange){
if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
mediaplayer.pause();
IsPaused = true;
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
}
else if(focusChange == AudioManager.AUDIOFOCUS_GAIN){
mediaplayer.start();
}
else if(focusChange == AudioManager.AUDIOFOCUS_LOSS){
if(mediaplayer != null)
{
mediaplayer.pause();
IsPaused = true;
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
}
}
}
};
public void showNotification() {
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SongsListActivity.class), 0);
Notification notification = new NotificationCompat.Builder(this)
.setTicker(NameD.getText())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(NameD.getText())
.setContentText(RateD.getText())
.setContentIntent(pi)
.setAutoCancel(false)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_songs_list);
Repeat = (ImageButton) findViewById(R.id.Repeat);
Next = (ImageButton) findViewById(R.id.Next);
Previous = (ImageButton) findViewById(R.id.Previous);
NameD = (TextView) findViewById(R.id.NameD);
ImageD = (ImageView) findViewById(R.id.ImageD);
RateD = (TextView) findViewById(R.id.RateD);
P_and_P = (ImageButton) findViewById(R.id.P_and_P);
Stop = (ImageButton) findViewById(R.id.Stop);
Duration = (TextView) findViewById(R.id.Duration);
Current_Time = (TextView) findViewById(R.id.Current_Time);
seekBar = (SeekBar) findViewById(R.id.SeekBar);
seekBar.setClickable(false);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Song = new ArrayList<>();
Song.add(new Songs("Alone", "Rate: 4.0", R.raw.alone, R.drawable.alone));
Song.add(new Songs("Anger", "Rate: 4.5", R.raw.anger, R.drawable.anger ));
Song.add(new Songs("Bad Situation", "Rate: 3.6", R.raw.bad_situation, R.drawable.badsitu));
Song.add(new Songs("Beni Soubi", "Rate: 4.0", R.raw.beni_soubi, R.drawable.beni));
Song.add(new Songs("Confronting", "Rate: 3.2", R.raw.confronting, R.drawable.confronting));
Song.add(new Songs("Daylight Of Konoha", "Rate: 4.7", R.raw.d_o_k, R.drawable.d_o_k ));
Song.add(new Songs("Dark Clouds", "Rate: 3.1", R.raw.dark_clouds, R.drawable.darkc));
Song.add(new Songs("Dept. To The Front Lines", "Rate: 5.0", R.raw.departure_to_the_front_lines, R.drawable.d_t_t_f_l));
Song.add(new Songs("Despair", "Rate: 3.6", R.raw.despair, R.drawable.despair));
Song.add(new Songs("Dokushinjutsu(Mt. Neoboku)", "Rate: 2.9", R.raw.dokushinjutsu, R.drawable.dokushin));
Song.add(new Songs("Emergence Of Talent", "Rate: 4.6", R.raw.emergence_of_talents, R.drawable.e_o_t));
Song.add(new Songs("Evening", "Rate: 3.4", R.raw.evening, R.drawable.evening));
Song.add(new Songs("Experienced Many Battles", "Rate: 4.0", R.raw.experienced_many_battles, R.drawable.e_m_b ));
Song.add(new Songs("Gekiha", "Rate: 4.0", R.raw.gekiha, R.drawable.gekiha ));
Song.add(new Songs("Genshi(Vision)", "Rate: 2.9", R.raw.genshi, R.drawable.genshi));
Song.add(new Songs("Girei(Pain's Theme)", "Rate: 4.5", R.raw.girei, R.drawable.girei));
Song.add(new Songs("Glued State", "Rate: 3.5", R.raw.glued_state, R.drawable.glue));
Song.add(new Songs("Guren", "Rate: 3.5", R.raw.guren, R.drawable.guren));
Song.add(new Songs("Heaven Shaking Event", "Rate: 5.0", R.raw.heaven_shaking_event, R.drawable.h_s_e));
Song.add(new Songs("Hidan", "Rate: 3.0", R.raw.hidan, R.drawable.hidan ));
Song.add(new Songs("Hidden Will To Fight", "Rate: 2.1", R.raw.hidden_will_to_fight, R.drawable.h_w_t_f));
Song.add(new Songs("Himoji", "Rate: 5.0", R.raw.himoji, R.drawable.himoji));
Song.add(new Songs("Hinata and Neji", "Rate: 5.0", R.raw.hinata_vs_neji, R.drawable.hi_vs_ne));
Song.add(new Songs("Homecoming", "Rate: 3.5", R.raw.homecoming, R.drawable.homecoming));
Song.add(new Songs("I said I'm Naruto", "Rate: 5.0", R.raw.i_said_i_am_naruto, R.drawable.i_s_i_a_n));
Song.add(new Songs("Jinchuuriki", "Rate: 2.5", R.raw.jinchuuriki, R.drawable.jinchu));
Song.add(new Songs("Kakuzu", "Rate: 5.0", R.raw.kakuzu, R.drawable.kakuzu));
Song.add(new Songs("Kokuten", "Rate: 5.0", R.raw.kokuten, R.drawable.kokuten));
Song.add(new Songs("Kouen", "Rate: 5.0", R.raw.kouen, R.drawable.kouen));
Song.add(new Songs("Nankou Furaku", "Rate: 5.0", R.raw.nankou_furaku, R.drawable.n_furu));
Song.add(new Songs("Naruto Main Theme", "Rate: 5.0", R.raw.naruto_main_theme, R.drawable.narutomt ));
Song.add(new Songs("Orochimaru's Theme", "Rate: 3.8", R.raw.orochimaru_theme, R.drawable.orochi));
Song.add(new Songs("Ranpatsu", "Rate: 5.0", R.raw.ranpatsu, R.drawable.ranpatsu));
Song.add(new Songs("Risking It All", "Rate: 5.0+", R.raw.risking_it_all, R.drawable.r_i_a));
Song.add(new Songs("Saika", "Rate: 4.9", R.raw.saika, R.drawable.saika));
Song.add(new Songs("Sasuke's Destiny", "Rate: 5.0", R.raw.sasuke_destiny, R.drawable.sasudesti));
Song.add(new Songs("Senya(Itachi's Theme)", "Rate: 5.0", R.raw.senya, R.drawable.senya ));
Song.add(new Songs("Shirotsumekusa", "Rate: 3.0", R.raw.shirotsumekusa, R.drawable.shirot ));
Song.add(new Songs("Shouryuu", "Rate: 5.0", R.raw.shouryuu, R.drawable.shouryuu));
Song.add(new Songs("Shippuuden", "Rate: 5.0", R.raw.shippuuden, R.drawable.shippuuden));
Song.add(new Songs("Turn Over", "Rate: 4.6", R.raw.turn_over, R.drawable.turn_over));
SongsAdapter SongAdapter = new SongsAdapter(this, Song, R.color.Main_Color);
ListView listView = (ListView) findViewById(R.id.list_Col);
listView.setAdapter(SongAdapter);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int ProgressChanged = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
ProgressChanged = i;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaplayer.seekTo(ProgressChanged);
}
});
Repeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Repeat_Value == 0) {
Repeat_Value = 1;
Repeat.setImageResource(R.drawable.ic_repeat_one_white_48dp);
} else {
Repeat_Value = 0;
Repeat.setImageResource(R.drawable.ic_repeat_white_48dp);
}
}
});
Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
if (II == (Song.size() - 1)) {
II = 0;
} else {
II += 1;
}
music_player(II);
}
}
}
);
Previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
if (II == 0) {
II = (Song.size() - 1);
} else {
II -= 1;
}
music_player(II);
}
}
}
);
P_and_P.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaplayer != null) {
if (!mediaplayer.isPlaying() || IsPaused) {
P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
IsPaused = false;
mediaplayer.start();
} else {
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
IsPaused = true;
mediaplayer.pause();
}
}
}
});
Stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaplayer != null) {
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
mediaplayer.pause();
mediaplayer.seekTo(0);
IsPaused = true;
}
}
});
listView.setOnItemClickListener
(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
Log.v("1", "Just upon entering onItemClick");
music_player(i);
showNotification();
}
}
);
}
public void music_player(int I) {
UpdateSongTime = new Runnable() {
public void run() {
startTime = mediaplayer.getCurrentPosition();
if ((TimeUnit.MILLISECONDS.toSeconds((long) startTime)) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime)) < 10) {
Current_Time.setText(String.format("0%d:0%d",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
} else {
Current_Time.setText(String.format("0%d:%d",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime))));
}
seekBar.setProgress((int) startTime);
myHandler.postDelayed(this, 100);
}
};
II = I;
IIII = II;
int Current_Song;
song = Song.get(I);
//If mediaPlayer is not used before, this will make oldsong as present song.
if (Old_Song == -326523) {
Old_Song = song.getSong();
}
Current_Song = song.getSong();
//If mediaPlayer is paused.
if (IsPaused) {
P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
//If the song paused is same as the new song.
if (Current_Song == Old_Song) {
mediaplayer.start();
NameD.setText(song.getNameOfSong());
RateD.setText(song.getDeveloperRate());
ImageD.setImageResource(song.getImage());
}
//If the song Paused is not the new song.
else {
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
}
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong());
seekBar.setMax(mediaplayer.getDuration());
Old_Song = song.getSong();
NameD.setText(song.getNameOfSong());
RateD.setText(song.getDeveloperRate());
ImageD.setImageResource(song.getImage());
mediaplayer.start();
if ((TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))) < 10) {
Duration.setText(String.format("0%d:0%d",
TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()),
TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))));
} else {
Duration.setText(String.format("0%d:%d",
TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()),
TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))));
}
seekBar.setProgress((int) startTime);
myHandler.postDelayed(UpdateSongTime, 100);
mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
IsPaused = true;
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
}
if (Repeat_Value == 0) {
if (II == (Song.size() - 1)) {
II = 0;
} else {
II += 1;
}
} else {
II = IIII;
}
IsPaused = false;
music_player(II);
}
});
}
}
IsPaused = false;
} else if (mediaplayer != null) {
//If mediaPlayer is already Playing a song.
if (mediaplayer.isPlaying()) {
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
mediaplayer.pause();
IsPaused = true;
if (Current_Song != Old_Song) {
NameD.setText(song.getNameOfSong());
RateD.setText(song.getDeveloperRate());
ImageD.setImageResource(song.getImage());
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
}
P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
IsPaused = false;
music_player(II);
}
}
}
//If mediaPlayer is used for first time and if mediaPlayer is neither paused
else {
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
}
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong());
seekBar.setMax(mediaplayer.getDuration());
Old_Song = song.getSong();
NameD.setText(song.getNameOfSong());
RateD.setText(song.getDeveloperRate());
ImageD.setImageResource(song.getImage());
mediaplayer.start();
if ((TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))) < 10) {
Duration.setText(String.format("0%d:0%d",
TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()),
TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))));
} else {
Duration.setText(String.format("0%d:%d",
TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()),
TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))));
}
seekBar.setProgress((int) startTime);
myHandler.postDelayed(UpdateSongTime, 100);
P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
IsPaused = true;
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
}
if (Repeat_Value == 0) {
if (II == (Song.size() - 1)) {
II = 0;
} else {
II += 1;
}
} else {
II = IIII;
}
IsPaused = false;
music_player(II);
}
});
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mediaplayer != null)
{
mediaplayer.release();
mediaplayer = null;
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
}
现在我想要的是,当点击通知时,SongsListActivity会启动,但会重新启动。
看,首先开始播放歌曲,然后更新搜索栏,更新歌曲的图像,名称等。
现在假设我按下导航栏上的后退按钮,然后不会破坏 活动,我再次打开应用程序,然后此活动不会被重置(也就是说,歌曲的所有名称,图像等都在离开活动之前显示),但是当我重复相同的过程时,但唯一的区别是我通过点击生成的通知打开活动;然后整个活动被重置(也就是说,活动看起来刚刚开始)。
我应该怎么做,以便活动不会重置,而是继续?
答案 0 :(得分:0)
一种解决方案是将更改保存在.txt文件中,并在您返回活动时重新加载它们。