`
从此醉
  • 浏览: 1044538 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

Intent在Android中的几种用法

 
阅读更多

转自东方尚智沈大海csdn博客:


如果是从BroadcastReceiver 启动一个新的Activity ,不要忘记i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

public class MyReceiver extends BroadcastReceiver{

public static final String action="acc";
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context,Receivered.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}


}

1. 指定act ion 和type
// SIM import
Intent importIntent = new Intent(Intent.ACTION_VIEW);
importIntent.setType("vnd.android.cursor.item/sim-contact");
importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
menu.add(0, 0, 0, R.string.importFromSim)
.setIcon(R.drawable.ic_menu_import_contact)
.setIntent(importIntent);

2. 指定act ion, da ta和type
(1)隐式查找type
示例代码:
uri: content://simcontacts/simPeople/(id)
intent = new Intent("android.intent.action.SIMEDIT",uri);
startActivity(intent);

程序会很据data中的uri去查找匹配的type(必须的)
provider中的getType()
case SIM_PEOPLE_ID:
return "vnd.android.cursor.item/sim-contact";

配置文件中的filter设定
AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.SIMEDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/sim-contact" />
</intent-filter>

也可以自己设定type,但只能使用 setDataAndType()

3. 其他设定intent的属性方式
Intent setComponent(ComponentName component)
Intent setClassName(Context packageContext, String className)
Intent setClassName(String packageName, String className)
Intent setClass(Context packageContext, Class<?> cls)

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=dsaddr=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. Intent it = new Intent(Intent.ACTION_VIEW);
  2. Uri uri = Uri.parse("file:///sdcard/song.mp3");
  3. it.setDataAndType(uri, "audio/mp3");
  4. 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

分享到:
评论

相关推荐

    Android Intent的几种用法全面总结

    Android IntAndroid Intent的几种用法全面总结ent的几种用法全面Android Intent的几种用法全面总结总结

    intent的常用方法

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

    Android Intent的几种用法详细解析

    下面列出几种Intent的用法显示网页: 代码如下:Uri uri = Uri.parse(“http://www.google.com”);Intent it = new Intent(Intent.ACTION_VIEW,uri);startActivity(it);显示地图: 代码如下:Uri uri

    详解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高级编程--源代码

    在每章的讲解中,它会让你通过一系列示例项目逐步掌握Android中的各种新功能和技术,助你取得最圆满的学习效果。本书所介绍的各个应用实例简明扼要且极具实用价值,它们覆盖了Android 1.0的所有基本功能和高级功能...

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

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

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

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

    android 面试2

     Message:消息的类型,在Handler类中的handleMessage方法中得到单个的消息进行处理  11. AIDL的全称是什么?如何工作?能处理哪些类型的数据?  答:全称是:Android Interface Define Language(android接口...

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

    每个 Android 应用程序都在 Dalvik VM 的一个实例中运行,这个实例驻留在一个由 Linux 内核管理的进程中,如下图所示。 图 2. Dalvik VM Android 应用程序由一个或多个组件组成: 活动 具有可视 UI 的应用...

    Android代码-ABridge

    更新说明:为了让用户能更灵活的使用ABridge进行进程间的通信,且不在局限于Activity使用场景,1.0.0版本做了全面的改进,可方便用户在进程中任何地方和另一个进程进行通信,同时也不在支持0.0.1的用法,给用户带来...

    Android 获取 usb 权限的两种方法

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

    android群雄传

    7.2.6 在XML中使用属性动画 170 7.2.7 View的animate方法 170 7.3 Android布局动画 171 7.4 Interpolators(插值器) 171 7.5 自定义动画 172 7.6 Android 5.X SVG矢量动画机制 175 7.6.1 标签 175 7.6.2 SVG...

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

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

    adb1.0.26包含fastboot.exe

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

    安卓android课程设计分析报告.doc

    产品的状况 1 Android是一种以Linux为基础的开放源代码操作系统,主要使用于便携设备。目前 未有统一中文名称,中国大陆地区较多人使用"安卓"或"安致"。Android操作系 统最初由AndyRubin开发,最初主要支持手机。...

Global site tag (gtag.js) - Google Analytics