Android基础

Intent

Intent类相当于Android平台中应用程序之间的通信网络;它具有:动作(String),类型(String),数据(Uri)三个属性,用于传递信息;在编译的时候,intent属于后期绑定(动态绑定),而不是在构建或编译时。

1.定义

按照对象的构造方法进行定义

2.调用

(1)显式调用:由用户进行定义:Intent(Context context,Class class)

public TestB extents Activity  
{  

.........  

};  

public class Test extends Activity  

{  

......  

public void switchActivity()  

{  

Intent i = new Intent(Test.this, TestB.class);  

this.startActivity(i);  

}  

} 

 

代码简洁明了,执行了switchActivity()函数,就会马上跳转到名为TestB的Activity中。

 (2)隐式调用:由平台来决定哪个组件最适合处理该intent;

隐式匹配,首先要匹配Intent的几项值:Action, Category, Data/Type,Component

如果填写了Componet就是上例中的Test.class)这就形成了显示匹配。所以此部分只讲前几种匹配,匹配规则为最大匹配规则。

(1.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在系统中是唯一的。

(1.2)data/type,你可以用Uri来做为data,比如Uri uri = Uri.parse();

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,

(1.3)至于分类Category,一般不要去在Intent中设置它,如果你写Intent的接收者,就在Manifest.xml的Activity的IntentFilter中包含android.category.DEFAULT,这样所有不设置Category(Intent.addCategory(String c);)的Intent都会与这个Category匹配。

(1.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);   

}  

} 

(1.5)定义IntentIFilter

IntentIFilter在Manifest XML文件中定义;它的定义要依靠于组件,用于规定组件接受的Intent的类型;filter过滤是前面讲到的intent的三个性质:动作、类型、数据;

<activity ....>

<intent-filter>

来看看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> 
原文地址:https://www.cnblogs.com/mengyan/p/2657978.html