Intent.createChooser(ntent target, CharSequence title)
其實 大傢對該功能第一影響就是ApiDemo 裡面的 其隻有區區幾行代碼 提取為:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setType(“audio/*”);
- startActivity(Intent.createChooser(intent, “Select music”));
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(“audio/*”);
startActivity(Intent.createChooser(intent, “Select music”));
執行之 會彈出一個對話框 效果為:
其實 對於這段代碼 大傢應該都能猜出什麼意思 現自己模擬並理解之
[代碼]
1. 定義TestActivity 用於根據傳入Uri 播放目標
public class TestActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle(“TestActivity”);
- Intent i = this.getIntent();
- Uri u = i.getData();
- try {
- playMusic(u);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
- MediaPlayer mp = new MediaPlayer();
- mp.setDataSource(this, uri);
- mp.prepare();
- mp.start();
- }
- }
public class TestActivity extends Activity {@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle(“TestActivity”);Intent i = this.getIntent();
Uri u = i.getData();
try {
playMusic(u);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}public void playMusic(Uri uri) throws IllegalArgumentException,
SecurityException, IllegalStateException, IOException{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(this, uri);
mp.prepare();
mp.start();
}
}
2. 在AndroidManifest 註冊TestActivity
<activity<