什麼是Intent:
Intent是Android中各個元件之間進行互動的一種重要方式
他不僅可以表示目前元件想要執行的步驟,而且還可以在不同元件之間傳遞資料
Intent的分類
大緻分為兩種:顯性Intent、隐性Intent
如何使用顯式Intent:Intent的意圖非常明顯
實作效果
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90zZOlXUU1UenRlT4FkaNVnVHFma5cVWv50MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2LcRHelR3LcJzLctmch1mclRXY39jNxYzMyQDMyETNyMDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
具體代碼實作:(為了提高我英語水準,我決定添加英語備注具體代碼)
1.修改FirstActivity的布局檔案:添加按鈕,設定id
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.activitytest.FirstActivity">
<Button
android:id="@+id/first_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="button 1"/>
</LinearLayout>
2.#####1.修改SecondActivity的布局檔案:添加按鈕,設定id
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.activitytest.SecondActivity">
<Button
android:id="@+id/second_btn"
android:layout_width="match_parent"
android:layout_height="50sp"
android:text="button 2" />
</LinearLayout>
3.修改FirstActivity活動:實作建立按鈕對象,設定監聽,實作跳轉
public class FirstActivity extends AppCompatActivity {
//建立按鈕對象 create button object
private Button startBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//綁定id bind id
startBtn = findViewById(R.id.first_btn);
//設定監聽 set monitor
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//實作跳轉
// achieve goto
//建立Intent對象 參數1:目前上下文環境,參數2:将要跳轉的活動頁面
// create Intent object (param1: current context param2:will goto context
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
//啟動跳轉 start goto
startActivity(intent);
}
});
}
}
over
如何使用隐式Intent:
隐式Intent的比較含蓄,他并不會明确指出将要啟動的活動,而是指定一系列更為抽象的action和category等資訊,然後由系統去分析intent的意圖,最後找到合适的活動去啟動。
執行個體示範:
效果展現:
具體代碼展現:
1.修改Manifest中的SecondActivity的啟動方式:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.activitytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
<!--修改SecondActivity的啟動方式 alter SecondActivity start mode-->
<intent-filter>
<!--添加action标簽,說明此活動隻能被這個action啟動-->
<!--add action label ,explain activity only start by this action -->
<action android:name="com.example.administrator.activitytest.ACTION_START"/>
<!--添加category标簽,兩個标簽同時滿足,才能啟動活動,但category.DEFAULT為預設category,是以啟動時不需要指定-->
<!--add category label,only two label satisfy,this activity can start,but category.DEFAULT is default,so not need point out-->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
修改FirstActivity中的啟動方式
public class FirstActivity extends AppCompatActivity {
//建立按鈕對象 create button object
private Button startBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//綁定id bind id
startBtn = findViewById(R.id.first_btn);
//設定監聽 set monitor
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//實作跳轉
// achieve goto
//建立Intent對象 參數1:啟動什麼樣的action
// create Intent object (param1: start what action)
Intent intent = new Intent("com.example.administrator.activitytest.ACTION_START");
startActivity(intent);
}
});
}
}
over
現在程式已經可以運作,不過如果在Menifest中添加一句自定義的category,應該如何啟動活動呢
需要注意的是每個Intent隻能指定一個action,但卻可以指定多個category,我們來添加一個自定義的吧
1.1.修改Manifest中的SecondActivity的啟動方式:
<intent-filter>
<!--添加action标簽,說明此活動隻能被這個action啟動-->
<!--add action label ,explain activity only start by this action -->
<action android:name="com.example.administrator.activitytest.ACTION_START"/>
<!--添加category标簽,兩個标簽同時滿足,才能啟動活動,但category.DEFAULT為預設category,是以啟動時不需要指定-->
<!--add category label,only two label satisfy,this activity can start,but category.DEFAULT is default,so not need point out-->
<category android:name="android.intent.category.DEFAULT"/>
<!--添加自定義的category add user defined category-->
<category android:name="com.example.administrator.activitytest.MY_CATEGORY"/>
</intent-filter>
2.修改FirstActivity中的啟動方式:
Intent intent = new Intent("com.example.administrator.activitytest.ACTION_START");
//添加自定義的category add user defined category
intent.addCategory("com.example.administrator.activitytest.MY_CATEGORY");
startActivity(intent);
over
其實隐式的Intent不僅可以調用自己的activity,還可以啟動其他程式的activity
效果展現:
代碼實作:隻需要在啟動當時上修改:
//建立Intent對象 參數:啟動什麼樣的action,ACTION_VIEW為系統内部的動作
// create Intent object (param1: start what action),ACTION_VIEW is system action
Intent intent = new Intent(Intent.ACTION_VIEW);
//給Intent設定資料,将網址通過parse解析然後來設定網址
//set Intent data ,put website parsed Uri object
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
為了精确的指定目前的活動相應什麼樣的資料,在Menifest添加如下資料
<intent-filter>
<!--添加action标簽,說明此活動隻能被這個action啟動-->
<!--add action label ,explain activity only start by this action -->
<action android:name="com.example.administrator.activitytest.ACTION_START"/>
<!--添加category标簽,兩個标簽同時滿足,才能啟動活動,但category.DEFAULT為預設category,是以啟動時不需要指定-->
<!--add category label,only two label satisfy,this activity can start,but category.DEFAULT is default,so not need point out-->
<category android:name="android.intent.category.DEFAULT"/>
<!--響應的資料類型為http response http data -->
<data android:scheme="http"/>
</intent-filter>