Android实验:广播实验
2023-12-13 04:18:41
广播
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
<?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=".ui.theme.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=".ui.theme.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>
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=".ui.theme.StaticReceiver">
<Button
android:id = "@+id/btnBroadcast"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "发送Broadcast"
android:onClick="send"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exp5"
tools:targetApi="31">
<activity
android:name=".ui.theme.mainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Exp5">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.exp5.ui.theme.StaticReceiver" />
<activity android:name="com.example.exp5.ui.theme.ActionReceiver" />
<receiver android:name="com.example.exp5.ui.theme.MyOrderReceiverOne" />
<receiver android:name="com.example.exp5.ui.theme.MyReceiver" />
<receiver android:name="com.example.exp5.ui.theme.MyOrderReceiverTwo" />
<receiver android:name="com.example.exp5.ui.theme.MyOrderReceiverThree" />
</application>
</manifest>
结果展示
暂且如此吧。
文章来源:https://blog.csdn.net/m0_72471315/article/details/134784752
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!