我怀疑只有一些愚蠢的错误,但是我被卡住了。问题很简单:我的Stringpath
未发送到MainActivity
。
在FileListActivity.class中创建意图
intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("path", fileName);
Toast.makeText(this, fileName + " set to play!", Toast.LENGTH_SHORT).show();
startActivity(intent);
在MainActivity.class中接收意图
protected void onResume(){
super.onResume();
Toast.makeText(this, "path set" + getIntent().getStringExtra("path"), Toast.LENGTH_SHORT).show();
if(getIntent().hasExtra("path")) try {
mediaPlayer.setDataSource(this, Uri.parse(getIntent().getStringExtra("path")));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
敬酒中FileListActivity
显示正确的文件名。在MainActivity
其中为null。
尝试覆盖protected void onNewIntent (Intent intent)
在MainActivity
如下...
@Override
protected void onNewIntent (Intent intent) {
setIntent(intent);
}
MainActivity
使用该方法重新启动Intent.FLAG_ACTIVITY_SINGLE_TOP
并调用getIntent()
该MainActivity
onResume()
方法将不会获得新的内容Intent
,而只会获得原始的内容Intent
,而不会增加您的path
额外费用。
通过覆盖onNewIntent(...)
并使用它来调用setIntent(...)
,新的Intent
覆盖原始的,对getIntent()
in的调用onResume()
应获取正确的数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句