Android中的Intent過濾器是什麼?
Intent過濾器是IntentFilter類的例項。在使用隱式Intent時,Intent過濾器非常有用。它不需要在Java程式碼中處理,而需要在AndroidManifest.xml檔案中設定。Android必須知道它正在啟動什麼樣的Intent,因此Intent過濾器向Android提供關於Intent和動作的資訊。
在啟動Intent之前,Android將進行動作測試、類別測試和資料測試。此示例演示如何在Android中使用Intent過濾器。
**步驟1** - 在Android Studio中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立一個新專案。
**步驟2** - 將以下程式碼新增到**res/layout/activity_main.xml**。
<?xml version="1.0" encoding="utf-8"?> <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" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/buton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="intent filter button" /> </LinearLayout>
在上面,我們添加了一個按鈕,當您單擊按鈕時,它將顯示帶有動作的Intent。
**步驟3** - 將以下程式碼新增到**src/MainActivity.java**
package com.example.andy.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
RadioButton radioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.buton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"contact@tutorialspoint.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Welcome to tutorialspoint.com");
startActivity(Intent.createChooser(intent, "Choose default Mail App"));
}
});
}
}在上面,當用戶單擊按鈕時,它將使用ACTION_SEND呼叫Intent,並將型別設定為message/rfc882。現在我們傳遞了電子郵件ID和主題訊息。
**步驟4** - 將以下程式碼新增到**manifest.xml**
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.andy.myapplication"> <uses-permission android:name="android.permission.INTERNET" /> <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=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="message/rfc822" /> </intent-filter> </activity> </application> </manifest>
在上面,我們聲明瞭action、category和data。讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟您的一個專案活動檔案,然後單擊執行
工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

單擊上面的按鈕,它將呼叫Intent選擇器以選擇應用程式來發送來自Intent的資料,如下所示 -

我們選擇了Gmail應用程式,如下所示 -

在上面的結果中,它將從Intent獲取資料並附加到Gmail應用程式。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP