intent-filter中的三个子选项的使用 action----intent-filter-------------data的说明

2024-01-16 04:32

本文主要是介绍intent-filter中的三个子选项的使用 action----intent-filter-------------data的说明,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用


转自 http://blog.csdn.net/chenzheng_java/article/details/6266135

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

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

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

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"));  
[java]  view plain copy
  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. }  
[java]  view plain copy
  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>  
[java]  view plain copy
  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.     }  
[java]  view plain copy
  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);

=============================================================================================================

以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent。

  一、打开一个网页,类别是Intent.ACTION_VIEW

  Uri uri = Uri.parse(“http://blog.3gstdy.com/”);

  Intent intent = new Intent(Intent.ACTION_VIEW, uri);

  二、打开地图并定位到一个点

  Uri uri = Uri.parse(“geo:52.76,-79.0342″);

  Intent intent = new Intent(Intent.ACTION_VIEW, uri);

  三、打开拨号界面 ,类型是Intent.ACTION_DIAL

  Uri uri = Uri.parse(“tel:10086″);

  Intent intent = new Intent(Intent.ACTION_DIAL, uri);

  四、直接拨打电话,与三不同的是,这个直接拨打电话,而不是打开拨号界面

  Uri uri = Uri.parse(“tel:10086″);

  Intent intent = new Intent(Intent.ACTION_CALL, uri);

  五、卸载一个应用,Intent的类别是Intent.ACTION_DELETE

  Uri uri = Uri.fromParts(“package”, “xxx”, null);

  Intent intent = new Intent(Intent.ACTION_DELETE, uri);

  六、安装应用程序 ,Intent的类别是Intent.ACTION_PACKAGE_ADDED

  Uri uri = Uri.fromParts(“package”, “xxx”, null);

  Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);

  七、播放音频文件

  Uri uri = Uri.parse(“file:///sdcard/download/everything.mp3″);

  Intent intent = new Intent(Intent.ACTION_VIEW, uri);

  intent.setType(“audio/mp3″);

  八、打开发邮件界面

  Uri uri= Uri.parse(“mailto:admin@3gstdy.com”);

  Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

  九、发邮件,与八不同这里是将邮件发送出去,

  Intent intent = new Intent(Intent.ACTION_SEND);

  String[] tos = { “admin@3gstdy.com ” };

  String[] ccs = { “webmaster@3gstdy.com ” };

  intent.putExtra(Intent.EXTRA_EMAIL, tos);

  intent.putExtra(Intent.EXTRA_CC, ccs);

  intent.putExtra(Intent.EXTRA_TEXT, “I come from http://blog.3gstdy.com ”);

  intent.putExtra(Intent.EXTRA_SUBJECT, “http://blog.3gstdy.com”);intent.setType(“message/rfc882″);

  Intent.createChooser(intent, “Choose Email Client”);

  //发送带附件的邮件

  Intent intent = new Intent(Intent.ACTION_SEND);

  intent.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);

  intent.putExtra(Intent.EXTRA_STREAM, “file:///sdcard/mysong.mp3″);

  intent.setType(“audio/mp3″);

  startActivity(Intent.createChooser(intent, “Choose Email Client”));

  十、发短信

  Uri uri= Uri.parse(“tel:10086″);

  Intent intent = new Intent(Intent.ACTION_VIEW, uri);

  intent.putExtra(“sms_body”, “I come from http://blog.3gstdy.com ”);

  intent.setType(“vnd.Android -dir/mms-sms”);

  十一、直接发邮件

  Uri uri= Uri.parse(“smsto://100861″);

  Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

  intent.putExtra(“sms_body”, “3g android http://blog.3gstdy.com ”);

  十二、发彩信

  Uri uri= Uri.parse(“content://media/external/images/media/23″);

  Intent intent = new Intent(Intent.ACTION_SEND);

  intent.putExtra(“sms_body”, “3g android http://blog.3gstdy.com ”);

  intent.putExtra(Intent.EXTRA_STREAM, uri);

  intent.setType(“image/png”);

  十三、# Market 相关

  1 //寻找某个应用

  Uri uri = Uri.parse(“market://search?q=pname:pkg_name”);

  Intent it = new Intent(Intent.ACTION_VIEW, uri);

  startActivity(it);

  //where pkg_name is the full package path for an application

  2 //显示某个应用的相关信息

  Uri uri = Uri.parse(“market://details?id=app_id”);

  Intent it = new Intent(Intent.ACTION_VIEW, uri);

  startActivity(it);

  //where app_id is the application ID, find the ID

  //by clicking on your application on Market home

  //page, and notice the ID from the address bar

  十四、路径规划

  Uri uri = Uri.parse(“http://maps.google .com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en”);

  Intent it = new Intent(Intent.ACTION_VIEW, uri);

  startActivity(it);

  //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456



这篇关于intent-filter中的三个子选项的使用 action----intent-filter-------------data的说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/611344

相关文章

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

java变量内存中存储的使用方式

《java变量内存中存储的使用方式》:本文主要介绍java变量内存中存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、变量的定义3、 变量的类型4、 变量的作用域5、 内存中的存储方式总结1、介绍在 Java 中,变量是用于存储程序中数据

关于Mybatis和JDBC的使用及区别

《关于Mybatis和JDBC的使用及区别》:本文主要介绍关于Mybatis和JDBC的使用及区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、JDBC1.1、流程1.2、优缺点2、MyBATis2.1、执行流程2.2、使用2.3、实现方式1、XML配置文件

macOS Sequoia 15.5 发布: 改进邮件和屏幕使用时间功能

《macOSSequoia15.5发布:改进邮件和屏幕使用时间功能》经过常规Beta测试后,新的macOSSequoia15.5现已公开发布,但重要的新功能将被保留到WWDC和... MACOS Sequoia 15.5 正式发布!本次更新为 Mac 用户带来了一系列功能强化、错误修复和安全性提升,进一步增