Android-broadcast-receivers
Android-放送受信機
'_Broadcast Receivers は、他のアプリケーションまたはシステム自体からのブロードキャストメッセージに単純に応答します。 これらのメッセージは、イベントまたはインテントと呼ばれることもあります。 たとえば、アプリケーションはブロードキャストを開始して、一部のデータがデバイスにダウンロードされて使用できることを他のアプリケーションに知らせることができるため、これはこの通信を傍受して適切なアクションを開始するブロードキャストレシーバーです。_
BroadcastReceiverをシステムブロードキャストインテントに対して機能させるには、次の2つの重要な手順があります-
- ブロードキャストレシーバーの作成。
- 放送受信機を登録する
カスタムインテントを実装する場合、追加のステップが1つあり、それらのインテントを作成してブロードキャストする必要があります。
ブロードキャストレシーバーの作成
ブロードキャストレシーバーは BroadcastReceiver クラスのサブクラスとして実装され、各メッセージが Intent オブジェクトパラメーターとして受信されるonReceive()メソッドをオーバーライドします。
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
放送受信機を登録する
アプリケーションは、_AndroidManifest.xml_ファイルにブロードキャストレシーバーを登録することにより、特定のブロードキャストインテントをリッスンします。 Androidシステムが起動プロセスを完了すると、システムによって起動されるシステム生成イベントACTION_BOOT_COMPLETEDの_MyReceiver_を登録することを検討してください。
放送受信機
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
これで、Androidデバイスが起動されるたびにBroadcastReceiver _MyReceiver_によってインターセプトされ、_onReceive()_内に実装されたロジックが実行されます。
*Intent* クラスの最終的な静的フィールドとして定義されたシステム生成イベントがいくつかあります。 次の表に、いくつかの重要なシステムイベントを示します。
Sr.No | Event Constant & Description |
---|---|
1 |
android.intent.action.BATTERY_CHANGED 充電状態、レベル、およびバッテリーに関するその他の情報を含むスティッキーブロードキャスト。 |
2 |
android.intent.action.BATTERY_LOW デバイスの低バッテリー状態を示します。 |
3 |
android.intent.action.BATTERY_OKAY バッテリーが少なくなった後、バッテリーが正常になったことを示します。 |
4 |
android.intent.action.BOOT_COMPLETED これは、システムの起動が完了した後、一度ブロードキャストされます。 |
5 |
android.intent.action.BUG_REPORT バグを報告するためのアクティビティを表示します。 |
6 |
android.intent.action.CALL データで指定された誰かに電話をかけます。 |
7 |
android.intent.action.CALL_BUTTON ユーザーが「呼び出し」ボタンを押して、ダイヤラーまたはその他の適切なUIに移動して呼び出しを行いました。 |
8 |
android.intent.action.DATE_CHANGED 日付が変更されました。 |
9 |
android.intent.action.REBOOT デバイスを再起動します。 |
カスタムインテントのブロードキャスト
アプリケーション自体でカスタムインテントを生成および送信する場合は、アクティビティクラス内で_sendBroadcast()_メソッドを使用して、それらのインテントを作成および送信する必要があります。 _sendStickyBroadcast(Intent)_メソッドを使用すると、Intentは sticky になります。つまり、送信している_Intent_は、ブロードキャストの完了後も残ります。
public void broadcastIntent(View view) {
Intent intent = new Intent();
intent.setAction("com.finddevguides.CUSTOM_INTENT");
sendBroadcast(intent);
}
このインテント_com.finddevguides.CUSTOM_INTENT_も、システムで生成されたインテントを登録したのと同様の方法で登録できます。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.finddevguides.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
</application>
例
この例では、_BroadcastReceiver_を作成してカスタムインテントをインターセプトする方法を説明します。 カスタムインテントに慣れたら、システム生成インテントをインターセプトするようにアプリケーションをプログラムできます。 したがって、次の手順に従って、_Hello World Example_の章で作成したAndroidアプリケーションを変更しましょう-
Step | Description |
---|---|
1 | You will use Android studio to create an Android application and name it as My Application under a package com.example.finddevguides7.myapplication as explained in the Hello World Example chapter. |
2 | Modify main activity file MainActivity.java to add broadcastIntent() method. |
3 | Create a new java file called MyReceiver.java under the package com.example.finddevguides7.myapplication to define a BroadcastReceiver. |
4 | An application can handle one or more custom and system intents without any restrictions. Every intent you want to intercept must be registered in your AndroidManifest.xml file using <receiver…/> tag |
5 | Modify the default content of res/layout/activity_main.xml file to include a button to broadcast intent. |
6 | No need to modify the string file, Android studio take care of string.xml file. |
7 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル MainActivity.java の内容です。 このファイルには、基本的なライフサイクルメソッドのそれぞれを含めることができます。 カスタムインテントをブロードキャストする_broadcastIntent()_メソッドを追加しました。
package com.example.finddevguides7.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
/* *Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//broadcast a custom intent.
public void broadcastIntent(View view){
Intent intent = new Intent();
intent.setAction("com.finddevguides.CUSTOM_INTENT"); sendBroadcast(intent);
}
}
以下は MyReceiver.java の内容です。
package com.example.finddevguides7.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
*Created by finddevguides7 on 8/23/2016.
*/
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
以下は、_AndroidManifest.xml_ファイルの変更されたコンテンツです。 ここに、サービスを含める<receiver …/>タグを追加しました。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finddevguides7.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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"/>
</intent-filter>
</activity>
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.finddevguides.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
以下は、カスタムインテントをブロードキャストするためのボタンを含む res/layout/activity_main.xml ファイルのコンテンツです。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of Broadcast"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="Broadcast Intent"
android:onClick="broadcastIntent"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
変更したばかりの* Hello World!アプリケーションを実行してみましょう。 環境のセットアップ中に *AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 Android StudioはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
次に、カスタムインテントをブロードキャストするために、 Broadcast Intent ボタンをクリックします。これにより、カスタムインテント_ "com.finddevguides.CUSTOM_INTENT" _がブロードキャストされます。これは、登録されたBroadcastReceiverによってインターセプトされます。 MyReceiverと実装されたロジックに従って、次のようにシミュレータの下部にトーストが表示されます-
他のBroadcastReceiverを実装して、システム起動、日付変更、バッテリー低下などのシステム生成インテントをインターセプトできます。