java–第一个页面
//按钮
public void bt(View v){
List<bean> list=new ArrayList<bean>();
Cursor cursor=getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()){
File file=new File(cursor.getString(cursor.getColumnIndex("_data")));
//获取音乐的名字
String name=cursor.getString(cursor.getColumnIndex("_display_name"));
//获取音乐大小
long size=cursor.getLong(cursor.getColumnIndex("_size"));
String size1=Formatter.formatFileSize(this, size);
list.add(new bean(file, name, size1));
}
Intent intent=new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("list", list.toArray());
startActivity(intent);
}
第二个页面
Intent intent=getIntent();
//接收集合
Object[] o=(Object[]) intent.getSerializableExtra("list");
for (Object object : o) {
list.add((bean)object);
}
OnItemClickListener lis=new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
bean be=list.get(arg2);
Intent intent=new Intent(Main2Activity.this,Main3Activity.class);
intent.putExtra("name", be.getName());
intent.putExtra("file", be.getFile().getPath());
startActivity(intent);
}
};
第三个页面播放音乐
Intent intent=getIntent();
tv=(TextView)findViewById(R.id.tname);
tv.setText(intent.getStringExtra("name"));
mp = new MediaPlayer();
try {
mp.setDataSource(intent.getStringExtra("file"));
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop(View v){
mp.stop();
}