Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Intent Filters trong Android là gì?

Intent filter là một thể hiện của lớp IntentFilter. Intent filters rất hữu ích khi sử dụng implicit intents, nó không được xử lý trong code Java, chúng ta phải thiết lập trong AndroidManifest.xml. Android phải biết loại intent nào đang được khởi chạy nên intent filters cung cấp thông tin cho Android về intent và actions.

Trước khi khởi chạy intent, Android sẽ thực hiện action test, category test và data test. Ví dụ này minh họa cách sử dụng intent filters trong Android.

Bước 1

Tạo một dự án mới trong Android Studio, vào File → New Project và điền đầy đủ thông tin cần thiết để tạo dự án mới.

Bước 2

Thêm mã sau vào res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"    xmlns:app="https://schemas.android.com/apk/res-auto"    xmlns:tools="https://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>

Ở trên, chúng ta có một button, khi nhấn vào nó sẽ hiển thị intent với action.

Bước 3

Thêm mã sau vào 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"));          }       }); } }

Trong mã trên, khi người dùng nhấn button, nó sẽ gọi intent sử dụng ACTION_SEND và đặt type là message/rfc822. Sau đó chúng ta truyền email và subject.

Bước 4

Thêm mã sau vào manifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://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>

Ở trên, chúng ta khai báo action, category và data. Hãy thử chạy ứng dụng. Giả sử bạn đã kết nối thiết bị Android thực tế với máy tính. Để chạy ứng dụng từ Android Studio, mở một file activity của dự án và nhấn biểu tượng Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn và kiểm tra thiết bị sẽ hiển thị màn hình mặc định:

Intent Filters trong Android là gì?

Nhấn vào button trên, nó sẽ gọi intent chooser để chọn ứng dụng gửi dữ liệu từ intent như hình dưới:

Intent Filters trong Android là gì?

Chúng ta chọn ứng dụng Gmail như hình dưới:

Intent Filters trong Android là gì?

Trong kết quả trên, nó sẽ lấy dữ liệu từ intent và điền vào ứng dụng Gmail.