我正在尝试开发一款音乐播放器应用程序。
首先,我从listview中选择一首音乐。然后音乐播放屏幕打开,显示选定的音乐。同时会出现一个通知。无论您身在何处,如果单击通知,音乐播放器屏幕都会显示当前音乐。然后,如果您愿意,您可以按后退按钮进入音乐列表。
关于API 19;在收到音乐播放屏幕的通知并按下后退按钮后,音乐列表会显示,但项目没有被点击。但API越高就没有问题。
唯一的麻烦;来自API 19上的通知面板。
我不能理解这个问题。下面是创建方法上的音乐列表。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showmusicfiles);
filesFound=(ListView)findViewById(R.id.files_found);
items = new ArrayList<FileItem>();
context = getApplicationContext();
String path = Environment.getExternalStorageDirectory() +
File.separator + "MusicApp"+File.separator+"mp3";
File f = new File(path);
final File fileList[] = f.listFiles();
for (File file : f.listFiles()) {
FileItem item=new FileItem();
item.setDescription(file.getName());
item.setFullPath(file.getAbsolutePath());
items.add(item);
}
searchResults=sortByDate(items);
adapter = new ArrayAdapter<FileItem>(getApplicationContext(), R.layout.music_file_item, searchResults){
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.music_file_item, parent, false);
}
TextView description = (TextView)convertView.findViewById(R.id.file_description);
final FileItem searchResult = searchResults.get(position);
description.setText(searchResult.getDescription().replace(".mp3",""));
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
deleteFileFromList(searchResults.get(position).getFullPath(),position);
break;
case DialogInterface.BUTTON_NEGATIVE:
toast("yoh dedın");
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(ShowMusicFiles.this);
builder.setMessage("Are you sure you want to delete").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
return true;
}
});
convertView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
FileTypeList = new ArrayList<File>();
FileTypeList = FileItemToFile(searchResults);
DescriptionList= new ArrayList<String>();
DescriptionList = getDescriptionList(searchResults);
playerIntent = new Intent(context, MusicPlayer.class);
playerIntent.putExtra("songPosition",position);
playerIntent.putExtra("songList",FileTypeList);
playerIntent.putExtra("descriptionList",DescriptionList);
playerIntent.putExtra("fromWhere","musicList");
playerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(playerIntent);
}
});
return convertView;
}
};
filesFound.setAdapter(adapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_music);
navigationView.setNavigationItemSelectedListener(this);
}
我在音乐播放屏幕上的反按方法只包括一个意向音乐列表页面。所以我觉得我不需要写音乐播放代码。
谢谢。
@Override
public void onBackPressed() {
Intent goToMusicList = new Intent(MusicPlayer.this,ShowMusicFiles.class);
startActivity(goToMusicList);
}
在filesFound.setAdapter(adapter)
之前调用filesFOund.invalidate()
。如果这还不能解决问题,请尝试将ShowMusicFiles活动的launchMode设置为singleTop。