欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android学习之Broadcast的简单使用

程序员文章站 2023-12-09 20:28:57
本文实例为大家分享了android学习之broadcast的使用方法,供大家参考,具体内容如下 实现开机启动提示网络的广播 package com.exampl...

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

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