目次
前回の記事 (必要に応じて移動してください) に引き続き、上記のシステム ブロードキャストの送信について学習し、次にカスタム ブロードキャストとローカル ブロードキャストについて学習しました。
1. カスタム ブロードキャストを送信する
1. 標準ブロードキャストを送信する
最初に、標準ブロードキャストを受信する準備が整ったブロードキャスト レシーバを定義します。
次のコードを使用して、新しい MyBroadcastReceiver クラスを作成します。
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "接收到广播", Toast.LENGTH_SHORT).show();
}
}
次に、AndroidManifest.xml でブロードキャスト レシーバーを変更します。
MyBroadcastReceiver が値が com.example.broadcasttest.MY_BROADCAST であるブロードキャストを受信するようにします。後でブロードキャストを送信する場合は、そのようなブロードキャストを送信する必要があります。
..........
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
...........
次のように activity_main.xml コードを変更します。
ブロードキャストを送信するためのボタンがここに追加されます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送广播"/>
</LinearLayout>
MainActivity コードを次のように変更します。
カスタム ブロードキャストを送信するロジックがボタンのクリック イベントに追加され、Intent オブジェクトが構築され、送信されるブロードキャストの値が渡され、Context の sendBroadcast() メソッドが呼び出されてブロードキャストが送信されます。 . このようにして、com.example.broadcasttest.MY_BROADCAST ブロードキャストをリッスンしているすべてのブロードキャスト レシーバーがメッセージを受信します。
ブロードキャストは Intent によって配信されるため、一部のデータは Intent で運ばれ、ブロードキャスト レシーバーに配信されます。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
}
});
}
}
効果は次のとおりです。
2.順番にブロードキャストを送信する
1) 順番放送を設定する
最初に MainActivity のコードを次のように変更します。
順序付きブロードキャストを送信するには、1 行のコードを変更するだけでよいことがわかります.sendOrderedBroadcast() メソッドには 2 つのパラメーターがあり、1 つ目はIntent で、2 つ目は権限に関連する文字列です。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendOrderedBroadcast(intent,null);
}
});
プログラムを実行した後、効果は以前と同じであることがわかります.違いは、この時点でブロードキャストレシーバーが正常であり、以前のブロードキャストレシーバーがブロードキャストを切り捨てることができることです.
2) では、優先度を設定する方法は?
登録に設定;
AndroidManifest.xml のコードを次のように変更します。
ここでは、優先度を 100 に設定しています。
.................
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
...................
3) ブロードキャストの通過を許可するかどうか
MyBroadcastReceiver コードを次のように変更します。
abortBroadcast() メソッドは、このブロードキャストを切り捨てることを意味し、後続のブロードキャスト レシーバはこのブロードキャストを受信できなくなります。
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "接收到广播", Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
2.ローカル放送を利用する
上記で使用されているブロードキャストはすべてシステム グローバル ブロードキャストです。つまり、送信されたブロードキャストは他のアプリケーションで受信できます。また、他のアプリケーションからブロードキャストを受信することもできます。これは、次のようなセキュリティ上の問題を引き起こしやすいものです。データのブロードキャストが他のアプリケーションによって傍受されたり、他のプログラムがさまざまな不要なブロードキャストを送信し続けたりする可能性があります。
上記の問題を解決するために、Anroid は一連のローカル ブロードキャスト メカニズムを導入し、このメカニズムを使用して送信されたブロードキャストはアプリケーション内でのみ送信できます。
実装してみましょう。
MainActivity コードを次のように変更します。
基本的には前の動的登録ブロードキャスト リスナーと同じです。
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localBroadcastManager = localBroadcastManager.getInstance(this);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);
}
});
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter);//注册本地广播监听器
}
protected void onDestroy(){
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "收到本地广播", Toast.LENGTH_SHORT).show();
}
}
}
注: ローカル ブロードキャストは、静的登録では受信できません。