欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > 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()中注册的接收广播属于动态注册。

上一篇:Android自定义动态壁纸开发(时钟)

栏    目:Android代码

下一篇:自定义Adapter并通过布局泵LayoutInflater抓取layout模板编辑每一个item实现思路

本文标题:Android学习之Broadcast的简单使用

本文地址:http://www.codeinn.net/misctech/19797.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有