Computer >> 컴퓨터 >  >> 프로그램 작성 >> Android

Android의 인텐트 필터란 무엇입니까?


인텐트 필터는 IntentFilter 클래스의 인스턴스입니다. 인텐트 필터는 암시적 인텐트를 사용하는 동안 유용합니다. Java 코드에서는 처리되지 않으므로 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를 사용하여 인텐트를 호출하고 유형을 message/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>

위에서 우리는 action, category 및 data를 선언했습니다. 응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 도구 모음에서 실행 아이콘을 클릭합니다. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오 -

Android의 인텐트 필터란 무엇입니까?

위의 버튼을 클릭하면 아래와 같이 인텐트에서 데이터를 보낼 애플리케이션을 선택하기 위해 인텐트 선택기를 호출합니다. -

Android의 인텐트 필터란 무엇입니까?

아래와 같이 Gmail 애플리케이션을 선택했습니다. -

Android의 인텐트 필터란 무엇입니까?

위의 결과에서 Intent에서 데이터를 가져와 Gmail 애플리케이션에 추가합니다.