【发布时间】:2018-09-09 23:58:11
【问题描述】:
我正在制作一个音乐应用程序,它会在播放音频时显示通知。单击时,此通知会通过意图打开主 (UI) 活动。
虽然这应该是一个非常简单的过程,但由于某种原因,无论我做什么,主要活动总是在按下通知时被破坏。我已经尝试过 singleTop、singleTask(作为意图标志和清单值)、保存实例状态包、onNewIntent,基本上是我能找到的任何接近解决方案的东西。但活动总是被破坏。我只能通过getIntent获取意图。
主要活动:https://pastebin.com/32uuK33E
音频服务:https://pastebin.com/ShiUfBMc
清单:https://pastebin.com/kPp7RSYK
因为“指向pastebin的链接必须附有代码”
这是意图(我已经尝试了所有相关标志的组合,所以我真的怀疑这是问题所在)
// creates an intent which opens the application when the notification is pressed
private PendingIntent getPendingIntent(Context context) {
Intent intent = getIntent(this);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(intent);
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
// returns an intent which resumes the Main Activity and passes various song information
private Intent getIntent(Context context) {
int duration = player != null ? player.getDuration() : 0;
boolean playing = player != null && player.isPlaying();
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra(MediaPlayerContract.INTENT_EXTRA_SONG_INFO, getSongInfo());
intent.putExtra(MediaPlayerContract.INTENT_EXTRA_POS, currentQueuePos);
intent.putExtra(MediaPlayerContract.INTENT_EXTRA_DURATION, duration);
intent.putExtra(MediaPlayerContract.INTENT_EXTRA_IS_PLAYING, playing);
return intent;
}
这是我试图读取数据的地方
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e(LOG_TAG, "flag got intent");
String[] info = intent.getStringArrayExtra(MediaPlayerContract.INTENT_EXTRA_SONG_INFO);
int pos = intent.getIntExtra(MediaPlayerContract.INTENT_EXTRA_POS, 0);
int duration = intent.getIntExtra(MediaPlayerContract.INTENT_EXTRA_DURATION, 0);
unpackageSongInfo(info);
currentQueuePos = pos;
seekBar.setMax(duration);
setIntent(intent);
}
【问题讨论】:
-
你好@JonDeal 检查此链接它将帮助您解决问题stackoverflow.com/questions/12043671/…
标签: java android android-activity service notifications