読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのBroadcastReceiver - Ordered Broadcast

Ordered Broadcastは

  1. 指定した順位で受信する(setPriority() Or android:priotiry)
  2. 次の受信者にデータ送信できる
  3. Broadcast Intent終了できる(abortBroadcast())

サンプルコード

① MainActivity.javaのボタンでOrdered Broadcastを行う

package com.example.liguofeng.myorderedbroadcast;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ボタンクリック
        ((Button) findViewById(R.id.broadcast)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.example.liguofeng.action.ORDERED_BROADCAST");
                intent.putExtra("msg1", "data from MainActivity.");

                // Ordered Broadcast
                sendOrderedBroadcast(intent, null);
            }
        });
    }
}

② MyFirstReceiverが受信し、Broadcastを終了する

package com.example.liguofeng.myorderedbroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyFirstReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,
                "IntentAction : " + intent.getAction() + "\n" +
                        "Message : " + intent.getStringExtra("msg1"),
                Toast.LENGTH_LONG)
                .show();
        Bundle data = new Bundle();
        data.putString("msg2", "data from MyfristReceiver.");

        // 処理結果
        setResultExtras(data);
    }
}

② MySecondReceiverが受信し、処理結果データを設定する

package com.example.liguofeng.myorderedbroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MySecondReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 前の処理結果
        Bundle data = getResultExtras(true);
        String first = data.getString("msg2");

        Toast.makeText(context,
                "Message1 : " + intent.getStringExtra("msg1") + "\n" +
                "Message2 : " + data.getString("msg2"),
                Toast.LENGTH_LONG)
                .show();
        // broadcast終了
        abortBroadcast();
    }
}

③ MyThirdReceiverは受信できない

package com.example.liguofeng.myorderedbroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyThirdReceiver extends BroadcastReceiver {
    public MyThirdReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,
                "これは表示されないね",
                Toast.LENGTH_LONG)
                .show();
    }
}

④ AndroiManifest.xmlに追加

<receiver
    android:name=".MyFirstReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="100">
        <action android:name="com.example.liguofeng.action.ORDERED_BROADCAST" />
    </intent-filter>
</receiver>
<receiver
    android:name=".MySecondReceiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter android:priority="50">
        <action android:name="com.example.liguofeng.action.ORDERED_BROADCAST" />
    </intent-filter>
</receiver>
<receiver
    android:name=".MyThirdReceiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter android:priority="10">
        <action android:name="com.example.liguofeng.action.ORDERED_BROADCAST" />
    </intent-filter>
</receiver>

f:id:liguofeng29:20151218010734g:plain