【实验名称】实验五、广播实验
【实验目的】
1、了解使用Intent进行组件通信的原理;
2、了解Intent过滤器的原理和匹配机制;
3、掌握发送和接收广播的方法
【实验内容】
任务1、普通广播;
任务2、系统广播;
任务3、有序广播;
【实验要求】
1、练习使用静态方法和动态方法注册广播接收器
2、练习发送广播消息的方法;
(请完成如下部分)
【实验设计】
静态注册
动态注册
MainActivity.java
package com.example.test04_broadcast;
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;
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);
}
public void orderSys(View view){
Intent intent = new Intent(MainActivity.this, OrderReceiver.class);
startActivity(intent);
}
}
ActionReceiver.java
package com.example.test04_broadcast;
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;
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);
}
}
MyOrderReceiverOne.java
package com.example.test04_broadcast;
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
package com.example.test04_broadcast;
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
package com.example.test04_broadcast;
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
package com.example.test04_broadcast;
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
package com.example.test04_broadcast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
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(),"com.example.test04_broadcast.MyReceiver"));
sendBroadcast(intent);
}
}
actionlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActionReceiver">
<Button
android:id = "@+id/actBroadcast"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "发送Broadcast"
android:onClick="sendAct"
/>
<Button
android:id = "@+id/registerReceiver"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "创建"
android:onClick="register"
/>
<Button
android:id = "@+id/unregisterReceiver"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "注销"
android:onClick="unregister"
/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id = "@+id/staticButton"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "静态方法"
android:onClick="staticSys"
/>
<Button
android:id = "@+id/actionButton"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "动态方法"
android:onClick="actionSys"
/>
<Button
android:id = "@+id/orderButton"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "有序广播"
android:onClick="orderSys"
/>
</LinearLayout>
orderlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrderReceiver">
<Button
android:id = "@+id/orderBroadcast"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "发送Broadcast"
android:onClick="sendOrd"
/>
</LinearLayout>
staticlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StaticReceiver">
<Button
android:id = "@+id/btnBroadcast"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "发送Broadcast"
android:onClick="send"
/>
</LinearLayout>
【实验分析或心得】
静态注册的优点是无论应用程序是否处于运行状态,广播接受者都会对程序进行监听。并在清单文件中完成注册。动态注册是创建一个广播接收者,并在Activity中进行注册,动态注册的广播接收者的生命周期依赖于注册广播的组件。当Activity销毁时,广播接收者也被移除。