Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(二)

来源:互联网 发布:如何建数据库 编辑:程序博客网 时间:2024/06/02 00:40
        其实对于这段代码大家应该都能猜出什么意思现自己模拟并理解

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

java代码:


  1. public class TestActivity extends Activity { 


  2. @Override 
  3. public void onCreate(Bundle savedInstanceState) { 
  4. super.onCreate(savedInstanceState); 
  5. setContentView(R.layout.main); 
  6. this.setTitle("TestActivity"); 


  7. Intent i = this.getIntent(); 
  8. Uri u = i.getData(); 


  9. try { 
  10. playMusic(u); 
  11. } catch (IllegalArgumentException e) { 
  12. // TODO Auto-generated catch block 
  13. e.printStackTrace(); 
  14. } catch (SecurityException e) { 
  15. // TODO Auto-generated catch block 
  16. e.printStackTrace(); 
  17. } catch (IllegalStateException e) { 
  18. // TODO Auto-generated catch block 
  19. e.printStackTrace(); 
  20. } catch (IOException e) { 
  21. // TODO Auto-generated catch block 
  22. e.printStackTrace(); 




  23. public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{ 
  24. MediaPlayer mp = new MediaPlayer(); 
  25. mp.setDataSource(this, uri); 
  26. mp.prepare(); 
  27. mp.start(); 


复制代码

       2. 在AndroidManifest注册TestActivity 

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

       3. 使用TestActivity

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

复制代码

4.png

原创粉丝点击