首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android学习-Intent学习

2012-08-14 
Android学习---Intent学习一、Intent的用途??? 首先,我们来看api的类总结:???? An intent is an abstract d

Android学习---Intent学习

一、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.compublic 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?)

1Uri?urireturnIt?=??new??2,地图

Uri?mapUri?=?Uri.returnIt?=??new??3,调拨打电话界面

Uri?telUri?=?Uri.returnIt?=??new??4,直接拨打电话

Uri?callUri?=?Uri.returnIt?=??new??5,卸载

Uri?uninstallUri?=?Uri.returnIt?=??new??6,安装

Uri?installUri?=?Uri.returnIt?=??new??7,播放

Uri?playUri?=?Uri.returnIt?=??new??8,掉用发邮件

Uri?emailUri?=?Uri.returnIt?=??new??9,发邮件

returnItString[]?tos?=?{?String[]?ccs?=?{?returnItreturnItreturnItreturnItreturnIt10,发短信

Uri?smsUri?=?Uri.returnIt?=??new??returnIt.putExtra(returnIt.setType(11,直接发邮件

Uri?smsToUri?=?Uri.returnIt?=??new??returnIt.putExtra(12,发彩信

UrreturnIt?=??new??returnIt.putExtra(returnIt.putExtra(returnIt.setType(?"image/png"?);



热点排行