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

首頁 > 系統 > Android > 正文

Android學習之Broadcast的簡單使用

2019-10-22 18:30:23
字體:
來源:轉載
供稿:網友

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

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 杭锦旗| 江都市| 威宁| 抚州市| 垫江县| 贵南县| 绍兴县| 洛阳市| 元谋县| 顺平县| 苗栗县| 壤塘县| 黎平县| 玉林市| 柳州市| 平果县| 桑日县| 民和| 鱼台县| 蒙阴县| 双柏县| 鹰潭市| 明溪县| 茶陵县| 临湘市| 云阳县| 贵定县| 屯门区| 永登县| 新乡县| 沁水县| 双鸭山市| 菏泽市| 玉林市| 东港市| 上思县| 乌海市| 玉门市| 盐津县| 屯昌县| 故城县|