分享

android之Itent.ACTION

 dmw_zgl 2014-11-21
  1. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);   
  2. intent.setType("audio/*");   
  3. startActivity(Intent.createChooser(intent, "Select music"));  
  1. <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
  2. intent.setType("audio/*");  
  3. startActivity(Intent.createChooser(intent, "Select music"));</span></span>  

 

 

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

 

 

 

 

 

 

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

 

 

 

[代码]

 

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

 

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

 

 

 

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>  
  1. <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;"><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></span></span>  

 

 

 

3. 使用TestActivity

 

Java代码
  1. public void sendChooser(){   
  2.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);   
  3.            
  4.         intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");   
  5.   
  6.         startActivity(Intent.createChooser(intent, "Select music1 app"));   
  7.     }  
  1. <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">public void sendChooser(){  
  2.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
  3.           
  4.         intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");  
  5.   
  6.         startActivity(Intent.createChooser(intent, "Select music1 app"));  
  7.     }</span></span>  

 

 

4. emulator 运行截图:

 

 

此外:

   //选择图片 requestCode 返回的标识

  Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"

  innerIntent.setType(contentType); //查看类型 String IMAGE_UNSPECIFIED = "image/*";

  Intent wrapperIntent = Intent.createChooser(innerIntent, null);

  ((Activity) context).startActivityForResult(wrapperIntent, requestCode);

 

  //视频

  Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);

  innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";

  Intent wrapperIntent = Intent.createChooser(innerIntent, null);

  ((Activity) context).startActivityForResult(wrapperIntent, requestCode);

 

  //添加音频

  Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);

  innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";

  Intent wrapperIntent = Intent.createChooser(innerIntent, null);

  ((Activity) context).startActivityForResult(wrapperIntent, requestCode);

 

  //录音

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

  intent.setType(ContentType.AUDIO_AMR); //String AUDIO_AMR = "audio/amr";

  intent.setClassName("com.android.soundrecorder",

  "com.android.soundrecorder.SoundRecorder");

  ((Activity) context).startActivityForResult(intent, requestCode);

 

  //拍摄视频

  int durationLimit = getVideoCaptureDurationLimit(); //SystemProperties.getInt("ro.media.enc.lprof.duration", 60);

  Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

  intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);

  intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit);

  intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);

  startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);

 

  //拍照 REQUEST_CODE_TAKE_PICTURE 为返回的标识

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //"android.media.action.IMAGE_CAPTURE";

  intent.putExtra(MediaStore.EXTRA_OUTPUT, Mms.ScrapSpace.CONTENT_URI); // output,Uri.parse("content://mms/scrapSpace");

  startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多