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

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

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

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. UML组件图(转载)

    概述: 组件图是不同的性质和行为.组件图用于模拟物理方面的系统. 现在的问题是什么,这些物理方面?物理方面的元素,如可执行文件,库,文件,证件等它位于在一个节点. 因此,组件图用于可视化的组织和系统组 ...

  2. poj 2187

    求凸包后枚举凸包上的点 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...

  3. 用于主题检测的临时日志(b42e98ba-eb4f-4099-a54c-7aee3f29c3dd - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(184c28c9-c88e-48fe-9713-6891e2d15044 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  4. 查看w3wp进程占用的内存及.NET内存泄露,死锁分析--转载

    一 基础知识 在分析之前,先上一张图: 从上面可以看到,这个w3wp进程占用了376M内存,启动了54个线程. 在使用windbg查看之前,看到的进程含有 *32 字样,意思是在64位机器上已32位方 ...

  5. swift循环

    ..< {     println(... {     println( ":"lala"] for (key,value)in dic {     println ...

  6. POJ 1936

    #include<iostream> #include<string> using namespace std; int main() { //freopen("ac ...

  7. 创建CancellationTokenSource对象用于取消Task

    虽然使用线程池ThreadPool让我们使用多线程变得容易,但是因为是由系统来分配的,如果想对线程做精细的控制就不太容易了,比如某个线程结束后执行一个回调方法.恰好Task可以实现这样的需求.这篇文章 ...

  8. Grub命令行

    今天电脑无缘无故无法正常启动,只提示 GRUB> 看来是GRUB引导出问题了,要解决下. 先 想到用制作U盘启动盘来启动,参照网上的方法,很简单用USBBOOT软件做了一个U盘启动盘,按F11在 ...

  9. Centos环境下部署游戏服务器-iptables

    简介:   图1        Centos做为服务器级操作系统,防火墙是不可缺少的.防火墙的主要功能为控制进出网络包,防火墙就如小区门卫的工作职责,检查出入小区居民的身份,如果不符合小区门卫管理条例 ...

  10. Windows环境变量设置无效解决办法——DOS窗口设置环境变量

    公司配置的电脑是Win7,使用的账户并不是管理员账户,我在计算机->属性中设置环境变量无效. 后来在DOS窗口中设置环境变量成功. 1. set [环境变量名称]=[所有环境变量值]:set P ...