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

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

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

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. WCF服务中,[DataMember]属性标记的属性一定要有set访问器

    WCF服务中,如果实体类中,包含有[DataMember]属性标记时,该属性一定要有set访问器.当系统必须调用到[DataMember]标记的属性时,如果该属性没有set访问器,则会出错.

  2. ssh 远程 centos 乱码

    今天,帮我们同学处理一下中文显示乱码的问题.这个是个国内Linux用户烦恼的问题,由于大部分的Linux发行版都是以英语为主体的,而且英文在通用性和稳定性上都比中文要好一些,各种奇怪的BUG也要少一点 ...

  3. Unity3D判断鼠标向右或向左滑动,响应不同的事件

    private var first = Vector2.zero; private var second = Vector2.zero; function Update () { } function ...

  4. close和shutdown的区别

    转的,没验证 close(sock_fd)会把sock_fd的内部计数器减1当sock_fd的内部计数器为0时, 才调用shutodwn(), 并最终释放文件描述符调用shutdown()只是进行了T ...

  5. C#&java重学笔记(变量与操作符)

    声明:自用参看读物 C#部分 1.C#中的byte和sbyte并不是二进制的比特,而是无符号 和 有符号的 8位整数. 2.decimal和float double都用来表示小数,前者用e的10的几次 ...

  6. iOS 隐藏顶部状态栏方式和更改颜色

    plist文件里面添加 AppDelegate: //显示状态栏 [[UIApplication sharedApplication]setStatusBarHidden:NO]; //将状态栏颜色设 ...

  7. POJ 2039

    #include<iostream> #include<stdio.h> #include<string> #define MAXN 20 using namesp ...

  8. 自动化 测试框架部署(python3+selenium2)

    安装Python 从https://www.python.org/downloads/下载最新版本的Python3,请注意,是3: 需要将Python的安装目录和安装目录下的Scripts文件夹添加到 ...

  9. junit中的assert方法总结

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...

  10. 【poj1284-Primitive Roots】欧拉函数-奇素数的原根个数

    http://poj.org/problem?id=1284 题意:给定一个奇素数p,求p的原根个数. 原根: { (xi mod p) | 1 <= i <= p-1 } is equa ...