Android学习之Broadcast的简单使用

文章TAG:Android BroadCast
时间:2017-09-12来源:www.aspku.com 作者:源码库 文章热度:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU源码库。


注:相关教程知识阅读请移步到Android开发频道。
相关Android开发
热门标签

Android开发Rss订阅Android开发搜索