国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android學習之Broadcast的簡單使用

2019-12-12 02:17:14
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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()中注冊的接收廣播屬于動態注冊。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇江市| 正阳县| 临夏县| 西丰县| 阿鲁科尔沁旗| 富蕴县| 广州市| 靖州| 萍乡市| 衡水市| 新丰县| 大方县| 同心县| 酒泉市| 礼泉县| 广汉市| 合川市| 奉新县| 安吉县| 浦东新区| 延庆县| 德江县| 西吉县| 奎屯市| 汝阳县| 甘孜| 东辽县| 文水县| 金乡县| 敖汉旗| 泰来县| 安庆市| 门头沟区| 临夏市| 兰考县| 永登县| 赤峰市| 南平市| 上蔡县| 清远市| 肃北|