当前位置:主页 > 移动开发 > Android代码 >

Android学习之Broadcast的简单使用

时间:2020-11-06 23:50:17 | 栏目:Android代码 | 点击:

本文实例为大家分享了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()中注册的接收广播属于动态注册。

您可能感兴趣的文章:

相关文章