你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢?

你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢?

如果,你想,那你是不是很像知道,我们应该怎么让其弹出来一张选择列表,又应该怎么代码实现后边的操作呢?

Itent.ACTION_PICK  Intent.ACTION_GET_CONTENT 两者都可以完成类似的功能,让我们一起来看下例子:

第一:Intent.ACTION_PICK

首先添加一个权限: 
<uses-permission android:name="android.permission.READ_CONTACTS"/> 
发起一个 Contact Picker 
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI); 
startActivityForResult(intent, PICK_CONTACT); 
重写方法 
@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data) 
  {  
      super.onActivityResult(reqCode, resultCode, data);  
        switch (reqCode) { 
             case (PICK_CONTACT) :      
             if (resultCode == Activity.RESULT_OK) {        
             Uri contactData = data.getData();        
             Cursor c =  managedQuery(contactData, null, null, null, null);        
             if (c.moveToFirst()) {          
                String name = c.getString(c.getColumnIndexOrThrow(People.NAME));          
                // TODO Whatever you want to do with the selected contact name.        
                                 }      
                      }      
               
      break;  
                
         } 
   }

例如 
String[] columns = new String[] {People.NAME}; 
int[] names = new int[] {R.id.row_entry}; 
mAdapter = new SimpleCursorAdapter(this, R.layout.mycontacts, C, columns, names); 
setListAdapter(mAdapter);

 
 
第二:Intent.ACTION_GET_CONTENT
 
我们可以发现,其实action_get_content是通过intent中设置的type属性来判断具体调用哪个程序的。
  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  播放目标

  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. }
  24. }
  25. public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
  26. MediaPlayer mp = new MediaPlayer();
  27. mp.setDataSource(this, uri);
  28. mp.prepare();
  29. mp.start();
  30. }
  31. }
  1. <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">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. }
  24. }
  25. public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
  26. MediaPlayer mp = new MediaPlayer();
  27. mp.setDataSource(this, uri);
  28. mp.prepare();
  29. mp.start();
  30. }
  31. }</span></span>

2. 在AndroidManifest 注册TestActivity

  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

  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"));
  5. }
  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. intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");
  4. startActivity(Intent.createChooser(intent, "Select music1 app"));
  5. }</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);

 

android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用的更多相关文章

  1. android脚步---Itent.ACTION_PICK ,startActivityForResult

    public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Intent.A ...

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

    大家对该功能第一印象就是ApiDemo 里面的 其只有区区几行代码  提取为: Intent intent = new Intent(Intent.ACTION_GET_CONTENT); inten ...

  3. android学习笔记29——Intent/IntentFilter

    Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...

  4. Android开发中使用Intent跳转到系统应用中的拨号界面、联系人界面、短信界面

    现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 首先,我们先看拨号界面,代码如下: Intent intent =new Intent(); intent. ...

  5. Android四大组件之Intent(续2)

    1.你如何通过一个intent来唤醒activity? this.startActivity(intent,request);      2.什么是显式.隐式的intents? 显式:指定组件名,通常 ...

  6. Android开发之常用Intent.Action【转】

    1.从google搜索内容 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putEx ...

  7. Android开发学习之Intent具体解释

    Intent简单介绍和具体解释:           Intent:协助应用间的交互与通信,Intent负责相应用中一次操作的动作.动作涉及的数据.附加数据进行描写叙述.               ...

  8. (Android数据传递)Intent消息传递机制 “Intent”“数据传递”

    Intent类的继承关系:   需要注意的是,该类实现了Parcelable(用于数据传递)和Cloneable接口. Intent是一种(系统级别的)消息传递机制,可以在应用程序内使用,也可以在应用 ...

  9. Android系统中标准Intent的使用

    Android系统用于Activity的标准Intent 1.根据联系人ID显示联系人信息= Intent intent=new Intent(); intent.setAction(Intent.A ...

随机推荐

  1. Swift-2-基本操作符

    // Playground - noun: a place where people can play import UIKit // 基本运算符 // 运算符有3种: 单目运算符(如 -a),二目运 ...

  2. .NET设计模式(15):结构型模式专题总结(转)

    摘要:结构型模式,顾名思义讨论的是类和对象的结构,它采用继承机制来组合接口或实现(类结构型模式),或者通过组合一些对象,从而实现新的功能(对象结构型模式).这些结构型模式,它们在某些方面具有很大的相似 ...

  3. PowerDesigner(四)-业务处理模型(转)

    业务处理模型 业务处理模型(Business Process Model,BPM)以业务需求作为出发点,用图形的方式描述系统的任务和业务流程,注重的是处理过程中数据流程.业务处理模型是从业务人员的角度 ...

  4. MyEclipse提示键配置、提示快捷键、提示背景色、关键字颜色、代码显示、编...

    1.提示键配置 一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选 ...

  5. Hadoop分布式配置

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 请先参照Linux安 ...

  6. IE如何兼容placeholder属性

    在前端开发中,经常需要为input设置placeholder属性,但是placeholder是HTML5新属性,在IE10以下不兼容,那么如何完美兼容呢? 网上搜索了一下,其实也挺简单的,可以采用以下 ...

  7. HTTP报头详解

    HTTP头字段包括4类:      general-header ; 通用报头      request-header ; 请求报头      response-header ; 响应报头      ...

  8. Sqli-labs less 18

    Less-18 本关我们这里从源代码直接了解到 对uname和passwd进行了check_input()函数的处理,所以我们在输入uname和passwd上进行注入是不行的,但是在代码中,我们看到了 ...

  9. eclipse下使用API操作HDFS

    1)使用eclipse,在HDFS上创建新目录 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Fil ...

  10. zend studio 快捷键收集

    Ctrl + /            单行注释 Ctrl + Shift + / 添加块注释 Ctrl + Shift + \   移除块注释 Ctrl + Shift + F 调整代码格式 Ctr ...