一、Intent的用途
首先,我们来看api的类总结:
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service. An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
intent是一个即将操作的一个抽象的描述。它能被startActivity方法调用去启动一个Activity,, broadcastIntent发送给任何对它感兴趣的
BroadcastReceiver
组件,和他也能用作 startService(Intent)
或者 bindService(Intent, ServiceConnection, int)与运行后台的Service进行交互。
一个Intent提供了一种实际的绑定在不同的应用程序。它最经常被用于启动Activity,我们可以认为它是不同的activities的粘合剂,其实从最基本来说它一种数据结构用来描述我们即将启动的Acitivy的描述。
二、Intent匹配
Intent传递过程中,要找到目标消费者(另一个Activity,IntentReceiver或Service),也就是Intent的响应者,有两种方法来匹配:
1.显示匹配(Explicit):
public TestB extents Activity { ......... }; public class Test extends Activity { ...... public void switchActivity() { Intent i = new Intent(Test.this, TestB.class); this.startActivity(i); } }
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里,传给电子邮件发送组件。
public class HelloActivity extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu); menu.add(0, Menu.FIRST+1, 1, R.string.menu_open); menu.add(0, Menu.FIRST+2, 2, R.string.menu_edit); menu.add(0, Menu.FIRST+3, 3, R.string.menu_update); menu.add(0, Menu.FIRST+4, 4, R.string.menu_close); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub super.onOptionsItemSelected(item); switch(item.getItemId()) { case Menu.FIRST + 1: { this.setTitle("Open Text!"); Intent i = new Intent(); i.setAction("test_action"); if (Tools.isIntentAvailable(this,i)) this.startActivity(i); else this.setTitle("the Intent is unavailable!!!"); break; } case Menu.FIRST + 2: { this.setTitle("Edit Text!"); break; } case Menu.FIRST + 3: { this.setTitle("Update Text!"); break; } case Menu.FIRST + 4: { this.setTitle("Close Text!"); break; } } return true; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); } } public class TestIntent extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Testing Intent here!"); this.setContentView(tv); } }
来看看TestIntent所在项目的Manifest.xml
<activity android:name="TestIntent" android:label="@string/hello"><intent-filter> <action android:name="test_action"></action> <category android:name="android.intent.category.DEFAULT"></category> </intent-filter> </activity> .....
四.用Intent调用系统中经常被用到的组件
此常用组件部分来源于(http://kuikui.javaeye.com/blog/318627 )
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:[email protected]" );
returnIt = new Intent (Intent . ACTION_SENDTO , emailUri);
9,发邮件
returnIt = new Intent (Intent . ACTION_SEND );
String[] tos = { "[email protected]" };
String[] ccs = { "[email protected]" };
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,发彩信
Ur i 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" );