Android学习之Broadcast的简单使用
作者:天秤心已随风去 时间:2023-11-19 15:18:02
本文实例为大家分享了Android学习之Broadcast的使用方法,供大家参考,具体内容如下
实现开机启动提示网络的广播
package com.example.luobo.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();//创建一个过滤器实例
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//添加接收CONNECTIVITY_CHANGE消息
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);//通过此方法获取ConnectivityManager实例
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();//调用实例connectivityManager的getActiveNetworkInfo()方法获取NetworkInfo实例
if (networkInfo != null && networkInfo.isAvailable()){
Toast.makeText(context,"Network is available",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context,"Network is unavailable",Toast.LENGTH_SHORT).show();
}
}
}
}
创建BootCompleteReceiver类
右击com.example.luobo.broadcasttest,New->Other->Broadcast,输入名字BootCompleteReceiver,勾选Enable,Exported,重写onReceive()方法。由于使用的是快捷方式创建的类,所需权限会在AndroidManifest.xml中自动注册。标签为receiver,但是还不够修改。
package com.example.luobo.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Boot Complete",Toast.LENGTH_SHORT).show();
}
}
在AndroidMaifest.xml注册权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luobo.broadcasttest">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />//注册接收网络消息广播
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>//注册接收开机启动广播
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>//开机时系统会发一条此广播
</intent-filter>
</receiver>
</application>
</manifest>
上述在AndroidMainfest.xml中注册接收广播消息属于静态注册,在OnCreate()中注册的接收广播属于动态注册。
标签:Android,Broadcast
0
投稿
猜你喜欢
java实现小猫钓鱼游戏
2021-10-10 19:59:48
Bean Searcher配合SpringBoot的使用详解
2022-06-21 23:49:00
Unity3D实现鼠标控制视角转动
2023-11-22 21:18:46
java异常(Exception)处理机制详解
2023-06-06 08:21:48
MybatisPlus多表连接查询的问题及解决方案
2023-11-25 22:09:06
Java虚拟机运行时栈的栈帧
2022-04-29 03:48:00
很简单的Java断点续传实现原理
2023-05-20 06:38:09
C#微信开发之接收 / 返回文本消息
2021-08-21 14:37:11
Java编程关于子类重写父类方法问题的理解
2022-05-25 05:00:32
JavaFX之TableView的使用详解
2022-07-16 06:32:24
Spring实战之Bean的作用域singleton和prototype用法分析
2023-03-23 20:48:37
c#定时运行程序分享(定时程序)
2023-12-11 20:47:31
c#实现断点续传功能示例分享
2022-05-03 04:39:49
详解Spring-Boot集成Spring session并存入redis
2021-10-13 07:48:30
基于C#中XmlWriter写入Xml的深入分析
2021-06-29 10:00:54
Android 实现会旋转的饼状统计图实例代码
2022-04-08 09:38:50
Java双冒号(::)运算符使用详解
2022-03-31 11:18:17
Android中双击返回键退出应用实例代码
2022-02-20 00:28:44
Java实现超大Excel文件解析(XSSF,SXSSF,easyExcel)
2023-11-27 11:02:17
java 读取网页内容的实例详解
2023-11-27 22:38:11