Intent 入门
Intent的中文意思是“意图,意向”,可以理解为不同组件之间通信的媒介或者信使。
目标组件需要通过intent来声明自己的条件,一般通过<intent-filter>元素来实现。Intent由以下几部分组成:动作(action),数据(data),分类(category),类型(Type),组件(Component)和扩展信息(Extra),通过这些可以启动其他组件并携带信息, Intent在寻找目标组件时有两种方法,第一通过组件名称直接指定,第二通过Intent filter过滤指定,以下为官方给出的Intent构造函数。
Intent()
Create an empty intent.
Intent(Intent o)
Copy constructor.
Intent(String action)
Create an intent with a given action.
Intent(String action, Uri uri)
Create an intent with a given action and for a given data url.
Intent(Context packageContext, Class<?> cls)
Create an intent for a specific component.
Intent(String action, Uri uri, Context packageContext, Class<?> cls)
Create an intent for a specific component with a specified action and data.
假设AndroidManifest.xml内容如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.synvata.aero"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".AeroNetApplication" >
<activity
android:label="@string/app_name"
android:name=".AeroNetActivity" >
</activity>
<activity android:name=".CityInfo" >
</activity>
<activity android:name=".InputActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TrackActivity" >
</activity>
<activity android:name=".ResultActivity" >
<intent-filter >
<action android:name="com.synvata.aero.RESULT_ACTION"></action>
<data android:scheme="content" android:path="com.synvata.aero/ResultActivity"/>
</intent-filter>
</activity>
<activity android:name=".BaseActivity" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
</manifest>
我们在TrackActivity中启动ResultActivity,方法如下
I Intent ComponentName属性
Intent intent = new Intent(TrackActivity.this,ResultActivity.class);
或者
ComponentName cn=new ComponentName(TrackActivity.this, ResultActivity.class);
Intent intent2=new Intent();
intent2.setComponent(cn);
// Bundle bundle = new Bundle();
// bundle.putString("starttime", starttime.getText().toString());
// bundle.putString("endtime", endtime.getText().toString());
// intent.putExtras(bundle);
// startActivity(intent);
II Intent的action属性
Action是指Intent要完成的动作,是一个常量字符串,我们在程序中定义,并在要访问的组件的Intent filter中声明就可以使用了,如上面中声明为com.synvata.aero.RESULT_ACTION。
Intent intent2=new Intent();
intent2.setAction("com.synvata.aero.RESULT_ACTION");
startActivity(intent2);
III Intent的data属性
Intent intent=new Intent();
Uri dataUri=Uri.parse("com.synvata.aero/ResultActivity");
intent.setData(dataUri);
Bundle bundle = new Bundle();
bundle.putString("starttime", starttime.getText().toString());
bundle.putString("endtime", endtime.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
以下是系统Action和data的一些组合使用
1.打开一个网页,类别是Intent.ACTION_VIEW
Uri uri = Uri.parse(“http://blog.3gstdy.com/”);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
2.打开地图并定位到一个点
Uri uri = Uri.parse(“geo:52.76,-79.0342″);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
3.打开拨号界面 ,类型是Intent.ACTION_DIAL
Uri uri = Uri.parse(“tel:10086″);
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
4.直接拨打电话,与三不同的是,这个直接拨打电话,而不是打开拨号界面
Uri uri = Uri.parse(“tel:10086″);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
5.卸载一个应用,Intent的类别是Intent.ACTION_DELETE
Uri uri = Uri.fromParts(“package”, “xxx”, null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
6.安装应用程序,Intent的类别是Intent.ACTION_PACKAGE_ADDED
Uri uri = Uri.fromParts(“package”, “xxx”, null);
Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);
7.播放音频文件
Uri uri = Uri.parse(“file:///sdcard/download/everything.mp3″);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setType(“audio/mp3″);
8.打开发邮件界面
Uri uri= Uri.parse(“mailto:admin@3gstdy.com”);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
9.发邮件,与八不同这里是将邮件发送出去,
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”));
10.发短信
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”);
11.直接发邮件
Uri uri= Uri.parse(“smsto://100861″);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra(“sms_body”, “3g android http://blog.3gstdy.com”);
12.发彩信
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”);
13.# 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
14、路径规划
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);

浙公网安备 33010602011771号