`
mmdev
  • 浏览: 13139480 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Intent在Android中的几种用法

 
阅读更多

如果是从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<wbr></wbr>ion 和type
// SIM imp<wbr></wbr>ort
Intent imp<wbr></wbr>ortIntent = new Intent(Intent.ACT<wbr></wbr>ION_VIEW);
imp<wbr></wbr>ortIntent.setType("vnd.android.cursor.item/sim-contact");
imp<wbr></wbr>ortIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
menu.add(0, 0, 0, R.string.imp<wbr></wbr>ortFromSim)
.setIcon(R.drawable.ic_menu_imp<wbr></wbr>ort_contact)
.setIntent(imp<wbr></wbr>ortIntent);

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

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

配置文件中的filter设定
AndroidManifest.xml
<intent-filter>
<act<wbr></wbr>ion android:name="android.intent.act<wbr></wbr>ion.SIMEDIT" />
<category android:name="android.intent.category.DEFAULT" />
<da<wbr></wbr>ta 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 Intent的几种常见用法的详细解释: 1. 显示网页: 当你想在设备上打开一个网页时,可以通过ACTION_VIEW Intent与系统浏览器交互。例如: ```java Uri uri = Uri.parse("http://www.google.com"); ...

    Android系列之Intent传递对象的几种实例方法

    本篇文章将详细探讨如何通过Intent在Android中传递对象,主要分为两种方式:使用`Serializable`接口和`Parcelable`接口。 1. **Serializable接口** `Serializable`是Java提供的一个标准序列化接口,当一个类实现了...

    Android中intent的使用

    本篇文章将深入探讨Intent的基本概念、类型、构造方法以及如何在Android中有效地使用Intent。 Intent是一种意图声明,表达了应用程序想要执行的操作。在Android系统中,Intent分为显式Intent和隐式Intent两种类型。...

    android中隐式intent的使用说明2022优秀文档.pptx

    在 Android 中使用隐式 Intent 需要在 AndroidManifest.xml 文件中,首先被调用的 Activity 要有一个带有 `&lt;intent-filter&gt;` 并且包含 `&lt;action&gt;` 的 Activity,设定它能处理的 Intent,并且 category 设为默认值。...

    intent的几种常用用法.pdf

    以下是对Intent几种常见用法的详细说明: 1. **显示网页** 使用`ACTION_VIEW`和`Uri`来打开浏览器并显示指定的网页。例如: ```java Uri uri = Uri.parse("http://www.google.com"); Intent it = new Intent...

    Android_Intent详解

    Intent Filter通常在`AndroidManifest.xml`文件中使用`&lt;intent-filter&gt;`标签进行声明。当系统接收到一个隐式Intent时,它会根据Intent中的Action、Data/Type、Category等信息来查找匹配的Intent Filter,并选择合适...

    Android提高第四篇之Activity+Intent.docx

    在Android应用开发中,Activity和Intent是两个至关重要的概念,它们构成了Android应用程序的基本骨架。Activity作为用户界面的主要载体,Intent则是连接各个组件的桥梁,负责传递消息和启动操作。 Activity的生命...

    [Android开发从零开始].7.Intent初级学习

    在Android应用开发中,Intent是一种强大的机制,它用于在组件之间传递消息,是应用程序中不同组件(如Activity、Service、BroadcastReceiver)之间交互的核心。在这个初级学习阶段,我们将深入理解Intent的基本概念...

    Android activity intent 相关Java源代码

    在Android应用开发中,Activity是Android系统中的一个核心组件,它是用户界面的载体,而Intent则是连接各个Activity的桥梁,用于传递数据和启动其他组件。Intent不仅用于启动Activity,还能启动Service、...

    android中activity跳转的几种动画

    本篇文章将深入探讨如何在Android中实现Activity跳转的几种动画效果。 一、默认动画 在不设置任何自定义动画的情况下,Android系统会使用默认的滑动效果进行Activity切换。这种效果可以通过设置`activity过渡`来...

    intent传递类内容

    在Android应用开发中,Intent是一种强大的机制,用于在应用程序组件之间进行通信,它承载着启动一个新活动、启动服务、传递数据等任务。Intent传递类内容主要包括以下几方面: 1. **基本Intent类型**: - **显式...

    Intent的简单使用

    在Android中,通常有以下几种方式: 1. **通过putExtra()和getExtra()方法**:在创建Intent时,可以使用`putExtra(String name, Parcelable value)`将数据作为额外参数添加到Intent中。在接收端,使用`getExtras()`...

    Android四大核心——Intent

    本篇将深入探讨Intent的基本概念、类型、使用方法及其在实际开发中的重要性。 **1. Intent基本概念** Intent是一个对象,用于表示应用程序中的一个动作,它描述了应用想要执行的操作以及可能需要的数据。Intent...

    Android Intent的几种用法详细解析

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

    android邮件发送几种方式

    以上就是Android中常用的几种邮件发送方式。根据实际需求和应用场景,开发者可以选择最适合的方法进行集成。在开发过程中,要注意用户隐私、安全性以及合规性问题,确保邮件发送功能的稳定和可靠。

    Xamarin.Android之Intent传递对象简单实例

    在Android应用开发中,Intent是一种强大的机制,用于在不同的组件之间进行通信,如活动(Activity)、服务(Service)以及广播接收器(BroadcastReceiver)。在Xamarin.Android中,我们可以利用C#的强大特性来处理...

    android中intent传递list或者对象的方法

    以上就是在Android中使用Intent传递List或对象的几种常见方法。选择哪种方法取决于你的需求,对于基本类型列表,直接使用Intent的方法;对于自定义对象,根据性能需求选择Serializable或Parcelable;如果需要全局...

    Android应用源码之Intent_TabHostSample.zip

    1. 初始化TabHost:首先,开发者会在onCreate()方法中实例化TabHost,并设置其为当前视图的根布局。 2. 创建TabSpec:然后,通过TabHost的newTabSpec()方法创建TabSpec,每个TabSpec都有一个标签名称和对应的Intent...

Global site tag (gtag.js) - Google Analytics