?1) activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="广播与服务"
android:textSize="20sp"
/>
</LinearLayout>
?2) MainActivity:
package com.example.z5;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(); //fasongguangbo
intent.setAction("send1");
intent.putExtra("msg","我是自定义广播");
// sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
Toast.makeText(MainActivity.this,"成功",Toast.LENGTH_LONG).show();
}
});
}
}
3)在AndroidMainfest中加入(顺便设优先级):
<receiver
android:name=".MyReceiver03"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="200">
<action android:name="send1" />
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver02"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="100">
<action android:name="send1" />
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver01"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="300">
<action android:name="send1" />
</intent-filter>
</receiver>
?4)接收
|)? MyReceive01:
package com.example.z5;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver01 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String data=intent.getStringExtra("msg");
Toast.makeText(context, "1接收"+data, Toast.LENGTH_SHORT).show();
// abortBroadcast();
}
}
||) MyReceiver03:
package com.example.z5;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver03 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String data=intent.getStringExtra("msg");
Toast.makeText(context, "3接收"+data, Toast.LENGTH_SHORT).show();
}
}
|||)MyReceiver02:
package com.example.z5;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver02 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String data=intent.getStringExtra("msg");
Toast.makeText(context, "2接收"+data, Toast.LENGTH_SHORT).show();
}
}
?
?
??
|