Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

ตัวกรองเจตนาใน Android คืออะไร


ตัวกรองเจตนาคืออินสแตนซ์ของคลาส IntentFilter ตัวกรอง Intent มีประโยชน์ในขณะที่ใช้ Intent โดยนัย มันจะไม่จัดการในโค้ดจาวา เราต้องตั้งค่าใน AndroidManifest.xml Android ต้องรู้ว่ามันเปิดตัวเจตนาแบบใด ดังนั้นตัวกรองเจตนาให้ข้อมูลแก่ Android เกี่ยวกับความตั้งใจและการกระทำ

ก่อนเปิดตัวเจตนา Android จะทำการทดสอบการกระทำ การทดสอบหมวดหมู่ และการทดสอบข้อมูล ตัวอย่างนี้สาธิตวิธีใช้ตัวกรองเจตนาใน Android

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน 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>

ในข้างต้น เราได้ให้ปุ่มเมื่อคุณคลิกที่ปุ่ม มันจะแสดงเจตนาด้วยการกระทำ

ขั้นตอนที่ 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 และจะตั้งค่าประเภทเป็นข้อความ/rfc882 ตอนนี้เราส่ง ID อีเมลและหัวเรื่องออกไปแล้ว

ขั้นตอนที่ 4 − เพิ่มรหัสต่อไปนี้ใน 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>

ในข้างต้น เราได้ประกาศการดำเนินการ หมวดหมู่ และข้อมูล มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

ตัวกรองเจตนาใน Android คืออะไร

คลิกที่ปุ่มด้านบนจะเรียกตัวเลือกความตั้งใจเพื่อเลือกแอปพลิเคชันเพื่อส่งข้อมูลจากความตั้งใจดังที่แสดงด้านล่าง -

ตัวกรองเจตนาใน Android คืออะไร

เราได้เลือกแอปพลิเคชั่น gmail ตามรูปด้านล่าง -

ตัวกรองเจตนาใน Android คืออะไร

จากผลข้างต้นจะเป็นการนำข้อมูลจาก Intent มาผนวกกับแอปพลิเคชัน Gmail