1 import android.content.BroadcastReceiver;
2 android.content.Context;
3 android.content.Intent;
4 android.widget.Toast;
5
6 public class MyReceiver extends BroadcastReceiver {
7
8 @Override
9 void onReceive(Context context,Intent intent) {
10 // TODO: This method is called when the BroadcastReceiver is receiving
11 Toast.makeText(context,"收到广播",Toast.LENGTH_SHORT).show();
12 }
13 }
1 <receiver
2 android:name=".receiver.MyReceiver"
3 android:enabled="true"
4 android:exported="true">
5 intent-filter6 action ="com.example.MyApplication2.myreceiver" />
7 </8 receiver>
1 Button
2 android:id="@+id/btn"
android:text="发送广播"
android:layout_width="match_parent"
5 android:layout_height="wrap_content" />
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.View;
5 android.widget.Button;
6
7 class MainActivity AppCompatActivity {
8
9 Button btn;
10 11 protected onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.activity_main);
14
15 btn = findViewById(R.id.btn);
16 btn.setOnClickListener(new View.OnClickListener() {
17 @Override
18 onClick(View view) {
19 Intent intent = new Intent("com.example.MyApplication2.myreceiver");
20 sendBroadcast(intent);
21 }
22 });
23
24 25 }
2 =".receiver.MyOrderReceiver1"
3 4 5 intent-filter android:priority="1000" 6 ="com.example.MyApplication2.myreceiver" 7 8 9 10 =".receiver.MyOrderReceiver2"
11 12 13 ="100"14 15 16 17 18 =".receiver.MyOrderReceiver3"
19 20 21 ="10"22 23 24 >
1 btn2 = findViewById(R.id.btn2);
2 btn2.setOnClickListener(3 4 5 Intent intent = new Intent("com.example.MyApplication2.myreceiver"6 sendOrderedBroadcast(intent,null7 8 });