`
kalogen
  • 浏览: 863535 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Android Intent的几种用法全面总结

 
阅读更多
Intent应该算是Android中特有的东西。你可以在Intent中指定程序要执行的动作(比如:view,edit,dial),以及程序执行到该动作时所需要的资料。都指定好后,只要调用startActivity(),Android系统会自动寻找最符合你指定要求的应用程序,并执行该程序。

下面列出几种Intent的用法
显示网页:
  1. Uri uri = Uri.parse("http://www.google.com");
  2. Intent it  = new Intent(Intent.ACTION_VIEW,uri);
  3. startActivity(it);
复制代码
显示地图:
  1. Uri uri = Uri.parse("geo:38.899533,-77.036476");
  2. Intent it = new Intent(Intent.Action_VIEW,uri);
  3. startActivity(it);
复制代码
路径规划:
  1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
  2. Intent it = new Intent(Intent.ACTION_VIEW,URI);
  3. startActivity(it);
复制代码
拨打电话:
调用拨号程序
  1. Uri uri = Uri.parse("tel:xxxxxx");
  2. Intent it = new Intent(Intent.ACTION_DIAL, uri);  
  3. startActivity(it);  
复制代码
  1. Uri uri = Uri.parse("tel.xxxxxx");
  2. Intent it =new Intent(Intent.ACTION_CALL,uri);
  3. 要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />
复制代码
发送SMS/MMS
调用发送短信的程序
  1. Intent it = new Intent(Intent.ACTION_VIEW);   
  2. it.putExtra("sms_body", "The SMS text");   
  3. it.setType("vnd.android-dir/mms-sms");   
  4. startActivity(it);  
复制代码
发送短信
  1. Uri uri = Uri.parse("smsto:0800000123");   
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
  3. it.putExtra("sms_body", "The SMS text");   
  4. startActivity(it);  
复制代码
发送彩信
  1. Uri uri = Uri.parse("content://media/external/images/media/23");   
  2. Intent it = new Intent(Intent.ACTION_SEND);   
  3. it.putExtra("sms_body", "some text");   
  4. it.putExtra(Intent.EXTRA_STREAM, uri);   
  5. it.setType("image/png");   
  6. startActivity(it);
复制代码
发送Email

  1. Uri uri = Uri.parse("mailto:xxx@abc.com");
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  3. startActivity(it);
复制代码
  1. Intent it = new Intent(Intent.ACTION_SEND);   
  2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");   
  3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
  4. it.setType("text/plain");   
  5. startActivity(Intent.createChooser(it, "Choose Email Client"));  
复制代码
  1. Intent it=new Intent(Intent.ACTION_SEND);     
  2. String[] tos={"me@abc.com"};     
  3. String[] ccs={"you@abc.com"};     
  4. it.putExtra(Intent.EXTRA_EMAIL, tos);     
  5. it.putExtra(Intent.EXTRA_CC, ccs);     
  6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");     
  7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
  8. it.setType("message/rfc822");     
  9. startActivity(Intent.createChooser(it, "Choose Email Client"));   
复制代码
添加附件
  1. Intent it = new Intent(Intent.ACTION_SEND);   
  2. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
  3. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");   
  4. sendIntent.setType("audio/mp3");   
  5. startActivity(Intent.createChooser(it, "Choose Email Client"));
复制代码
播放多媒体
  1.   
  2. Intent it = new Intent(Intent.ACTION_VIEW);
  3. Uri uri = Uri.parse("file:///sdcard/song.mp3");
  4. it.setDataAndType(uri, "audio/mp3");
  5. startActivity(it);
复制代码
  1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
  3. startActivity(it);  
复制代码
Uninstall 程序
  1. Uri uri = Uri.fromParts("package", strPackageName, null);   
  2. Intent it = new Intent(Intent.ACTION_DELETE, uri);   
  3. startActivity(it);
复制代码

uninstall apk

  1. Uri uninstallUri = Uri.fromParts("package", "xxx", null);

  2. returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
复制代码


install apk

  1. Uri installUri = Uri.fromParts("package", "xxx", null);

  2. returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
复制代码



play audio

  1. Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");

  2. returnIt = new Intent(Intent.ACTION_VIEW, playUri);
复制代码
  1. //发送附件
  2. Intent it = new Intent(Intent.ACTION_SEND);  
  3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
  4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3");  
  5. sendIntent.setType("audio/mp3");  
  6. startActivity(Intent.createChooser(it, "Choose Email Client"));
  1. //搜索应用
  2. Uri uri = Uri.parse("market://search?q=pname:pkg_name");  
  3. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  4. startActivity(it);  
  5. //where pkg_name is the full package path for an application  

  6. //显示指定应用的详细页面(这个好像不支持了,找不到app_id)
  7. Uri uri = Uri.parse("market://details?id=app_id");  
  8. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  9. startActivity(it);  
  10. //where app_id is the application ID, find the ID  
  11. //by clicking on your application on Market home  
  12. //page, and notice the ID from the address bar


/**
     * 获得包安装Intent
     * 
@param tempFile
     * 
@return
     
*/

    
public static Intent getPackageInstallIntent(File tempFile)
    
{
        Uri mPackageURI 
= Uri.fromFile(tempFile);
        Intent in 
= new Intent();
        in.setAction(Intent.ACTION_VIEW);
        in.addCategory(Intent.CATEGORY_DEFAULT);
        in
                .setComponent(
new ComponentName(
                        
"com.android.packageinstaller",
                        
"com.android.packageinstaller.PackageInstallerActivity"));
        in.setDataAndType(mPackageURI,
                
"application/vnd.android.package-archive");
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        
return in;
    }



分享到:
评论

相关推荐

    Android Intent的几种用法详细解析

    主要介绍了Android Intent的几种用法,有需要的朋友可以参考一下

    intent的常用方法

    intent的常用方法 Intent在Android中的几种用法 文章分类:综合技术

    详解Android中Intent的使用方法

    Intent主要有以下几种重要用途: 1. 启动Activity:可以将Intent对象传递给startActivity()方法或startActivityForResult()方法以启动一个Activity,该Intent对象包含了要启动的Activity的信息及其他必要的数据。 2...

    新版Android开发教程.rar

    的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...

    Android移动应用开发实验指导书.docx

    (2)掌握Intent的几种常用的属性。 (3)Android系统内置Intent的使用。 (4)了解Activity的生命周期 实验软、硬件环境 硬件:PC电脑一台; 配置:winxp或win7系统,内存大于4G,硬盘250G及以上 JDK1.7 、Eclipse...

    Android高级编程--源代码

    作为使用androidsdk构建这些应用程序的实用指南书籍,《android高级编程》从始至终穿插了一系列示例项目,每个项目都引入android的新功能和新技术,以助您达到最圆满的学习效果。书中介绍android的所有基本功能,并...

    Android Activity之间的数据传递方法总结

    前言 在Activity间传递的数据一般比较简单,但是有时候实际开发中也会传一些比较复杂的数据,本节一起来学习更多...下面将通过几个小栗子分别介绍一下这几种方法。 1.1、基本数据类型传递 String 不是基本数据类型,

    Android移动应用开发实验指导书.docx.docx

    (2)掌握Intent的几种常用的属性。 (3)Android系统内置Intent的使用。 (4)了解Activity的生命周期 实验软、硬件环境 硬件:PC电脑一台; 配置:winxp或win7系统,内存大于4G,硬盘250G及以上 JDK1.7 、Eclipse...

    Android代码-ABridge

    跨进程常见的几种通信方式:Bundle通过Intent传递数据,文件共享,ContentProvider,基于Binder的AIDL和Messenger以及Socket。 三、IPC是what? 也许有些小伙伴还不是很清楚IPC概念,这里我简单的概述一下。 IPC是 ...

    Google Android SDK开发范例大全(完整版)

    G1 问世几个月之后,随后就发布了 Android Market,它使用户可以浏览应用程序,并且可以将应用程序直接下载到他们的手机上。经过大约 18 个月,一个新的移动平台进入公众领域。 ---------------------------------...

    android群雄传

    2.2 Android Studio高级使用技巧 19 2.2.1 更新SDK 20 2.2.2 Android Studio常用界面 21 2.2.3 导入Android Studio工程 23 2.3 ADB命令使用技巧 24 2.3.1 ADB基础 24 2.3.2 ADB常用命令 25 2.3.3 ADB命令来源...

    Android 获取 usb 权限的两种方法

    最近工作上遇到几个USB模块在android平台上适配使用的情况,所以要用到USB权限获取问题 ##USB权限获取有以下2种方式: 一、直接在AndroidManifest.xml文件中进行如下配置: &lt;activity android:name=....

    android 面试2

    17、Android中使用Menu时可能需要重写的方法(AC) A、onCreateOptionsMenu() B、onCreateMenu() C、onOptionsItemSelected() D、onItemSelected() 18、关于ContentValues类的说法正确的是(A) A、他和HashTable比较...

    adb1.0.26包含fastboot.exe

    输出格式为 [serialNumber] [state],serialNumber 即我们常说的 SN,state 有如下几种: offline —— 表示设备未连接成功或无响应。 device —— 设备已连接。注意这个状态并不能标识 Android 系统已经完全启动...

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--缓存优化之几种方案lastModified |--缓存优化之本地缓存优化(超过规定值或SD卡容量不够时) |--网络post提交查询请求 |--网络之HttpClient的get和post用法 |--网络之判断网络状态是否可用 |--网络之设置apn |--...

Global site tag (gtag.js) - Google Analytics