您的位置:首页 > 其它

Intent的应用(一):隐式意图和显式意图

2017-11-21 22:59 357 查看
* 显式意图

能从intent上直观的看到跳转到哪一个界面

应用场景: 一般是自己内部跳转的时候,用显式意图 效率高

* 隐式意图

要指定action(动作) 、data(数据) 来达到跳转的目的

应用场景:一般是跳转到其他应用中的某个界面,或者自己的应用界面想被其他应用打开。 效率低

第一步:编写布局XML文件和Activity文件

第二步:在清单文件中注册Activity
第三步:跳转

第一步:编写布局XML文件和Activity文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33ff0000"
android:orientation="vertical" >

<TextView
android:text="隐式跳转的界面"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
package com.heima.testIntent;

import android.app.Activity;
import android.os.Bundle;

public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_second);
}
}
第二步:在清单文件中注册Activity
<!-- 显式意图注册 -->
<activity android:name="com.heima.testIntent.SecondActivity"></activity>

<!-- 隐式意图注册 -->
<activity android:name="com.heima.testIntent.FirstActivity">
<!-- 意图过滤 器-->
<!-- action和 category都必须写上 -->
<intent-filter>
<action android:name="com.heima.testIntent.action.bb"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
注意:只要activity中有<intent-filter>属性,那么该Activity能被其他应用程序访问
第三步:跳转
public void click01(View v) {

//使用显式手法启动界面
Intent intent = new Intent(MainActivity.this,SecondActivity.class);

startActivity(intent);
}

public void click02(View v) {

//使用隐式手法启动02界面

Intent intent = new Intent();
intent.setAction("com.heima.testIntent.action.bb");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: