Android 应用与 Android 系统和其他 Android 应用之间可以相互收发广播消息,这与发布-订阅设计模式相似。这些广播会在所关注的事件发生时发送。举例来说,Android 系统会在发生各种系统事件时发送广播,例如系统启动或设备开始充电时。再比如,应用可以发送自定义广播来通知其他应用它们可能感兴趣的事件(例如,一些新数据已下载)。
应用可以注册接收特定的广播。广播发出后,系统会自动将广播传送给同意接收这种广播的应用。
一般来说,广播可作为跨应用和普通用户流之外的消息传递系统。但是,您必须小心,不要滥用在后台响应广播和运行作业的机会,因为这会导致系统变慢
关于详细的广播介绍什么的就不在这里叙述了,贴一个笔者认为写的详尽且通俗的文章
linkhttp://t.csdnimg.cn/wRnOF感兴趣可以自行了解
1、 了解使用Intent进行组件通信的原理;
2、 了解Intent过滤器的原理和匹配机制;
3、 掌握发送和接收广播的方法
任务1、普通广播;
任务2、系统广播;
任务3、有序广播;
1、练习使用静态方法和动态方法注册广播接收器
2、练习发送广播消息的方法;
ActionReceiver.java
import android.app.Activity;
//import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import com.example.exp5.R;
public class ActionReceiver extends Activity {
protected static final String ACTION =
"com.eoeAndroid.broadcastReceiver.RECEIVER_ACTION";
private MyReceiver myReceiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actionlayout);
}
public void sendAct(View view){
Intent intent=new Intent(); //实例化Intent
intent.setAction(ACTION); //设置Intent的action属性
intent.putExtra("info","动态方法");
sendBroadcast(intent);
}
public void register(View view){
myReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
registerReceiver(myReceiver, filter);
}
public void unregister(View view){
unregisterReceiver(myReceiver);
}
}
mainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.exp5.R;
public class mainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void staticSys(View view){
Intent intent = new Intent(mainActivity.this, StaticReceiver.class);
startActivity(intent);
}
public void actionSys(View view){
Intent intent = new Intent(mainActivity.this, ActionReceiver.class);
startActivity(intent);
}
MyOrderReceiverOne.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyOrderReceiverOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("我收到有序的啦");
}
}
MyOrderReceiverTwo.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyOrderReceiverTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("我收到有序的啦");
}
}
MyOrderReceiverThree.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyOrderReceiverThree extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("我收到有序的啦");
}
}
MyReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast t = Toast.makeText(context,"广播方式:"+intent.getStringExtra("info"), Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP,0,40);
t.show();
}
}
StaticReceiver.java
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.example.exp5.R;
public class StaticReceiver extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.staticlayout);
}
public void send(View view){
Intent intent = new Intent();
//intent.setAction("com.eoeAndroid.broadcastReceiver.RECEIVER_ACTION");
intent.putExtra("info","静态方法");
intent.setComponent(new ComponentName(getPackageName(),"D:/Android/exp/exp5/app/src/main/java/com/example/exp5/ui/theme/MyReceiver.java"));
sendBroadcast(intent);
}
}
actionlayout.xml
activity_main.xml
staticlayout.xml
AndroidManifest.xml
暂且如此吧。