1,显示匹配(Explicit)
01 | public TestB extents Activity |
05 | public class Test extends Activity |
08 | public void switchActivity() |
10 | Intent i = new Intent(Test.this, TestB.class); |
2,隐式匹配(Implicit): 隐式匹配,首先要匹配Intent的几项值:Action, Category, Data/Type,Component。如果填写了Componet就是上例中的Test.class)这就形成了显示匹配。所以此部分只讲前几种匹配。匹配规则为最大匹配规则, 1,如果你填写了Action,如果有一个程序的Manifest.xml中的某一个Activity的IntentFilter段中定义了包含了相同的Action那么这个Intent就与这个目标Action匹配,如果这个Filter段中没有定义Type,Category,那么这个Activity就匹配了。但是如果手机中有两个以上的程序匹配,那么就会弹出一个对话可框来提示说明。 Action的值在Android中有很多预定义,如果你想直接转到你自己定义的Intent接收者,你可以在接收者的IntentFilter中加入一个自定义的Action值(同时要设定Category值为"android.intent.category.DEFAULT"),在你的Intent中设定该值为Intent的Action,就直接能跳转到你自己的Intent接收者中。因为这个Action在系统中是唯一的。 2,data/type,你可以用Uri来做为data,比如Uri uri = Uri.parse(http://www.google.com); Intent i = new Intent(Intent.ACTION_VIEW,uri);手机的Intent分发过程中,会根据http://www.google.com 的scheme判断出数据类型type 手机的Brower则能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能处理http:的type, 3,至于分类Category,一般不要去在Intent中设置它,如果你写Intent的接收者,就在Manifest.xml的Activity的IntentFilter中包含android.category.DEFAULT,这样所有不设置Category(Intent.addCategory(String c);)的Intent都会与这个Category匹配。 4,extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。 用 Intent 激活电话拨号程序 这里使用一个Intent打开电话拨号程序,Intent的行为是ACTION_DIAL,同时在Intent中传递被呼叫人的电话号码。 在用户界面中加入一个Button按钮,编辑res/layout/main.xml文件:
01 | <?xml version="1.0" encoding="utf-8"?> |
03 | xmlns:android="http://schemas.android.com/apk/res/android" |
04 | android:orientation="vertical" |
05 | android:layout_width="fill_parent" |
06 | android:layout_height="fill_parent" |
09 | android:id = "@+id/button_id" |
10 | android:layout_width="fill_parent" |
11 | android:layout_height="wrap_content" |
12 | android:text="@string/button" |
14 | </LinearLayout><SPAN style="FONT-FAMILY: verdana, 'courier new'; FONT-SIZE: 14pt"><SPAN style="WHITE-SPACE: normal"> |
我们把Button的id设置为button_id, 同时将Button显示在界面上的文字设置为res/string.xml/下的Button,打开res/string.xml,把button的内容设置为“拨号”:
1 | <?xml version="1.0" encoding="utf-8"?> |
3 | <string name="button">拨号</string> |
4 | <string name="app_name">TinyDialer</string> |
5 | </resources><SPAN style="FONT-FAMILY: verdana, 'courier new'; FONT-SIZE: 14pt"><SPAN style="WHITE-SPACE: normal"> |
创建TinyDialer的Activity
01 | public class TinyDialer extends Activity { |
02 | /** Called when the Activity is first created. */ |
04 | public void onCreate(Bundle savedInstanceState) { |
05 | super.onCreate(savedInstanceState); |
06 | setContentView(R.layout.main); |
08 | final Button button = (Button) findViewById(R.id.button_id); |
09 | button.setOnClickListener(new Button.OnClickListener() { |
11 | public void onClick(View b) { |
12 | Intent i = new Intent(Intent.ACTION_DIAL, |
13 | Uri.parse("tel://13800138000")); |
用Intent调用系统中经常被用到的组件
1,web浏览器
Uri uri= Uri.parse("http://kuikui.javaeye.com");
returnIt = new Intent(Intent.ACTION_VIEW, uri);
2,地图
Uri mapUri = Uri.parse("geo:38.899533,-77.036476");
returnIt = new Intent(Intent.ACTION_VIEW, mapUri);
3,调拨打电话界面
Uri telUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_DIAL, telUri);
4,直接拨打电话
Uri callUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_CALL, callUri);
5,卸载
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
6,安装
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
7,播放
Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
8,掉用发邮件
Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com");
returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);
9,发邮件
returnIt = new Intent(Intent.ACTION_SEND);
String[] tos = { "shenrenkui@gmail.com" };
String[] ccs = { "shenrenkui@gmail.com" };
returnIt.putExtra(Intent.EXTRA_EMAIL, tos);
returnIt.putExtra(Intent.EXTRA_CC, ccs);
returnIt.putExtra(Intent.EXTRA_TEXT, "body");
returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");
returnIt.setType("message/rfc882");
Intent.createChooser(returnIt, "Choose Email Client");
10,发短信
Uri smsUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_VIEW, smsUri);
returnIt.putExtra("sms_body", "shenrenkui");
returnIt.setType("vnd.android-dir/mms-sms");
11,直接发邮件
Uri smsToUri = Uri.parse("smsto://100861");
returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);
returnIt.putExtra("sms_body", "shenrenkui");
12,发彩信
Uri mmsUri = Uri.parse("content://media/external/images/media/23");
returnIt = new Intent(Intent.ACTION_SEND);
returnIt.putExtra("sms_body", "shenrenkui");
returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);
returnIt.setType("image/png");