Android 开发之:Intent.createChooser() 妙用

Intent.createChooser(ntent target, CharSequence title)

其实 大家对该功能第一影响就是ApiDemo 里面的 其只有区区几行代码  提取为:

Intent intent =  new  Intent(Intent.ACTION_GET_CONTENT);   

  1. intent.setType( "audio/*" );   
  2. startActivity(Intent.createChooser(intent,  "Select music" ));  

 

执行之 会弹出一个对话框 效果为:

其实 对于这段代码 大家应该都能猜出什么意思  现自己模拟并理解之

[代码]

1. 定义TestActivity 用于根据传入Uri  播放目标

public   class  TestActivity  extends  Activity {   
  1.        
  2.      @Override   
  3.      public   void  onCreate(Bundle savedInstanceState) {   
  4.          super .onCreate(savedInstanceState);   
  5.         setContentView(R.layout.main);   
  6.          this .setTitle( "TestActivity" );   
  7.            
  8.         Intent i =  this .getIntent();   
  9.            
  10.         Uri u = i.getData();   
  11.            
  12.          try  {   
  13.             playMusic(u);   
  14.         }  catch  (IllegalArgumentException e) {   
  15.              // TODO Auto-generated catch block   
  16.             e.printStackTrace();   
  17.         }  catch  (SecurityException e) {   
  18.              // TODO Auto-generated catch block   
  19.             e.printStackTrace();   
  20.         }  catch  (IllegalStateException e) {   
  21.              // TODO Auto-generated catch block   
  22.             e.printStackTrace();   
  23.         }  catch  (IOException e) {   
  24.              // TODO Auto-generated catch block   
  25.             e.printStackTrace();   
  26.         }   
  27.     }   
  28.        
  29.      public   void  playMusic(Uri uri)  throws  IllegalArgumentException, SecurityException, IllegalStateException, IOException{   
  30.         MediaPlayer mp =  new  MediaPlayer();   
  31.         mp.setDataSource( this , uri);   
  32.         mp.prepare();   
  33.         mp.start();   
  34.     }   
  35. }  

2. 在AndroidManifest 注册TestActivity

< activity   android:name = ".TestActivity"   android:label = "TestActivity" >   
  1.      < intent-filter >   
  2.              < action   android:name = "android.intent.action.GET_CONTENT"   />   
  3.       < category   android:name = "android.intent.category.DEFAULT"   />   
  4.       < category   android:name = "android.intent.category.OPENABLE"   />   
  5.       < data   android:mimeType = "audio/music1"   />   
  6.      </ intent-filter >   
  7. </ activity >   

3. 使用TestActivity

public   void  sendChooser(){   
  1.     Intent intent =  new  Intent(Intent.ACTION_GET_CONTENT);   
  2.     intent.setDataAndType(Uri.parse( "file:///sdcard/DCIM/cc.mp3" ),  "audio/music1" );   
  3.     startActivity(Intent.createChooser(intent,  "Select music1 app" ));   
  4. }  

4. emulator 运行截图:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值