Android
 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 −次のコードをma​​nifest.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からアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

Androidのインテントフィルターとは何ですか?

上記のボタンをクリックすると、インテントチューザーが呼び出され、以下に示すように、インテントからデータを送信するアプリケーションを選択します-

Androidのインテントフィルターとは何ですか?

以下に示すようにGmailアプリケーションを選択しました-

Androidのインテントフィルターとは何ですか?

上記の結果では、インテントからデータを取得し、Gmailアプリケーションに追加します。


  1. AndroidのLinkifyTextviewとは何ですか?

    例に入る前に、linkifyとは何かを知っておく必要があります。 Linkifyは、HTMLのハイパーリンクに似ています。それを使用して、コンテンツを閲覧できます。これは、Androidでlinkifyとtextviewを使用する簡単なソリューションです。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 上記のXMLでは、textviewを

  2. Androidのコンテキストとは何ですか?

    定義 これは、アプリケーション/オブジェクトの現在の状態のコンテキストです。これにより、新しく作成されたオブジェクトが何が起こっているのかを理解できます。通常、これを呼び出して、プログラムの別の部分(アクティビティおよびパッケージ/アプリケーション)に関する情報を取得します。以下のプログラムでは、textViewを動的に作成し、コンテキストを渡したことがわかります。このコンテキストは、環境に関する情報を取得するために使用されます。 この例は、AndroidtextViewでコンテキストを表示する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し