Android Intent.createChooser() 妙用

Intent.createChooser(ntent target, CharSequence title)android

  其實 你們對該功能第一影響就是ApiDemo 裏面的 其只有區區幾行代碼 提取爲:app

  Java代碼ide

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);this

  intent.setType("audio/*");get

  startActivity(Intent.createChooser(intent, "Select music"));it

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);io

  intent.setType("audio/*");class

  startActivity(Intent.createChooser(intent, "Select music"));file

  執行之 會彈出一個對話框 效果爲:im

  其實 對於這段代碼 你們應該都能猜出什麼意思 現本身模擬並理解之

  [代碼]

  1. 定義TestActivity 用於根據傳入Uri 播放目標

  Java代碼

  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); 

  Intent.createChooser(ntent target, CharSequence title)

  其實 你們對該功能第一影響就是ApiDemo 裏面的 其只有區區幾行代碼 提取爲:

  Java代碼

  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 播放目標

  Java代碼

  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

  Java代碼

  < activity android:name=".TestActivity"

  android:label="TestActivity">

  < intent-filter>

  < action android:name="android.intent.action.GET_CONTENT" />

  < category android:name="android.intent.category.DEFAULT" />

  < category android:name="android.intent.category.OPENABLE" />

  < data android:mimeType="audio/music1" />

  < /intent-filter>

  < /activity>

  < activity android:name=".TestActivity"

  android:label="TestActivity">

  < intent-filter>

  < action android:name="android.intent.action.GET_CONTENT" />

  < category android:name="android.intent.category.DEFAULT" />

  < category android:name="android.intent.category.OPENABLE" />

  < data android:mimeType="audio/music1" />

  < /intent-filter>

  < /activity>

  3. 使用TestActivity

  Java代碼

  public void sendChooser(){

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

  intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");

  startActivity(Intent.createChooser(intent, "Select music1 app"));

  }

  public void sendChooser(){

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

  intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");

  startActivity(Intent.createChooser(intent, "Select music1 app"));

  }

  4. emulator 運行截圖:

相關文章
相關標籤/搜索