Android中Intent的显示和隐式使用
Android应用程序中组件之间的通信都少不了Intent的使用,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。intent就是意图的意思。Intent分两种:显式(Explicit intent)和隐式(Implicit intent)。
显示调用Intent
简单的Demo从一个Activity转到另外一个Aactivity:
Mainactivity的布局文件
1
2
3
4
5
6
7
8
9
10
11
12
|
<EditText android:id= "@+id/edt_content"
android:layout_width= "match_parent"
android:layout_height= "wrap_content" />
<Button android:id= "@+id/btn_login"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_below= "@id/edt_content"
android:onClick= "login"
android:text= "查询" />
|
Mainactivity中调用点击事件:
1
2
3
4
|
EditText contentEditText=(EditText) findViewById(R.id.edt_content); Intent intent= new Intent( this ,PersonActivity. class );
intent.putExtra(EXTRA,contentEditText.getText().toString());
startActivity(intent);
|
这个时候的Intent就是显示调用,直接指定了接收参数的Activity,可以唯一确定一个Activity,意图特别明确,这个时候需要在PersonActivity接收参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class PersonActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
TextView textView=(TextView) findViewById(R.id.txt_content);
Intent intent=getIntent();
String str=intent.getStringExtra(MainActivity.EXTRA);
textView.setText(str);
textView.setTextSize(20);
textView.setTextColor(Color.RED);
}
|
另外这个时候传递的参数使用的方法是putExtra,如果传递的参数比较多可以使用Bundle类似于map。
隐式调用
隐式,即不是像显式的那样直接指定需要调用的Activity,隐式不明确指定启动哪个Activity,而是设置Action、Data、Category,让系统来筛选出合适的Activity。筛选是根据所有的<intent-filter>来筛选。
这个时候需要在AndroidManifest.xml中设置一下intent-filter中去设置一下,如下,Category直接使用默认的就行:
1
2
3
4
|
<intent-filter> <action android:name= "com.example.googleone.Peson" />
<category android:name= "android.intent.category.DEFAULT" />
</intent-filter>
|
Mainactivity中的调用使用,这个时候的调用:
1
2
|
Intent intent= new Intent( "com.example.googleone.Peson" );
startActivity(intent);
|
这个自己定义的Action字符串可以调用自身程序的Activity,还可以其他应用程序的Action,比如说常用的拨号面板:
1
2
|
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
|
如果这个时候在AndroidManifest.xml文件中给PersonActivity, 加一个Action,如下:
1
2
3
4
5
6
7
8
9
|
<activity android:name= ".PersonActivity"
android:label= "@string/title_activity_person" >
<intent-filter>
<action android:name= "android.intent.action.DIAL" />
<action android:name= "com.example.googleone.Peson" />
<category android:name= "android.intent.category.DEFAULT" />
</intent-filter>
</activity>
|
Mainactivity中的调用:
1
2
3
4
5
6
7
8
9
10
11
|
Intent intent= new Intent(Intent.ACTION_DIAL);
if (intent.resolveActivity(getPackageManager()) == null )
{
view.setEnabled( false );
}
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// TODO Auto-generated catch block
Toast.makeText( this , "找不到对应的Activity" ,Toast.LENGTH_SHORT).show();
}
|
结果如图所示:
Intent.ACTION_DIAL是系统常量字符串,等价于android.intent.action.DIAL,调用的时候通过这个action的名称,去寻找具有这个action的activity~
本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4033299.html,如需转载请自行联系原作者