▼Androidメモ▼
ブロードキャストレシーバー


ブロードキャストレシーバーでインテントを受信するプログラムを作成する。

Android 3.0以降ではデフォルトで停止中のアプリはブロードキャストを受け取らない。
以下のフラグを指定することで受け取れるようになる。
「intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);」
 

インテント
インテントの主な用途は「アクティビティの起動」と「インテントレシーバーへの通知」。

インテントレシーバーへの通知を行うには、putExtra()メソッドでパラメータを設定後、sendBroadcast()でブロードキャストする。
通知先の指定は「暗黙的なインテント」となる。
アクティビティの起動は1つのアクティビティに対して行うが、ブロードキャストレシーバーへの通知は複数のブロードキャストレシーバーに対して行う。

ソースコード
IntentReceiverEx.java
package net.npaka.intentreceiverex;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

//インテントレシーバー
public class IntentReceiverEx extends Activity
implements View.OnClickListener {
private Button btnCall;//呼び出しボタン

//初期化
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);

//レイアウトの生成
LinearLayout layout=new LinearLayout(this);
layout.setBackgroundColor(Color.rgb(255,255,255));
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);

//ボタンの生成
btnCall=new Button(this);
btnCall.setText("インテントのブロードキャスト");
btnCall.setOnClickListener(this);
setLLParams(btnCall);
layout.addView(btnCall);
}

//ボタンクリックイベントの処理
public void onClick(View v) {
if (v==btnCall) {
//インテントのブロードキャスト
Intent intent=new Intent("net.npaka.intentreceiverex.VIEW");
intent.putExtra("TEXT","ブロードキャストレシーバーのテストです");
sendBroadcast(intent);
}
}

//ライナーレイアウトのパラメータ指定
private static void setLLParams(View view) {
view.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}

TextReceiver.java
package net.npaka.intentreceiverex;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

//テキストレシーバー
public class TextReceiver extends BroadcastReceiver {
//インテントの受信
@Override
public void onReceive(Context context,Intent intent) {
//パラメータの取得
Bundle bundle=intent.getExtras();
String text=bundle.getString("TEXT");

//トーストの表示
showToast(context,text);
}

//トーストの表示
private static void showToast(Context context,String text) {
Toast toast=Toast.makeText(context,text,Toast.LENGTH_SHORT);
toast.show();
}
}


AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.npaka.intentreceiverex"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">

<activity
android:name=".IntentReceiverEx"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name="TextReceiver">
<intent-filter>
<action android:name="net.npaka.intentreceiverex.VIEW" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>


−戻る−