最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android學習之Broadcast的簡單使用

 更新時間:2017年08月09日 10:11:56   作者:天秤心已隨風去  
這篇文章主要為大家詳細介紹了Android學習之Broadcast的簡單使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android學習之Broadcast的使用方法,供大家參考,具體內容如下

實現(xiàn)開機啟動提示網絡的廣播

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();//創(chuàng)建一個過濾器實例
  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();
   }
  }
 }
}

創(chuàng)建BootCompleteReceiver類

右擊com.example.luobo.broadcasttest,New->Other->Broadcast,輸入名字BootCompleteReceiver,勾選Enable,Exported,重寫onReceive()方法。由于使用的是快捷方式創(chuàng)建的類,所需權限會在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"/>//開機時系統(tǒng)會發(fā)一條此廣播
   </intent-filter>
  </receiver>
 </application>

</manifest>

上述在AndroidMainfest.xml中注冊接收廣播消息屬于靜態(tài)注冊,在OnCreate()中注冊的接收廣播屬于動態(tài)注冊。

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

相關文章

最新評論

琼海市| 苏尼特左旗| 哈尔滨市| 昂仁县| 韶关市| 营山县| 金坛市| 固阳县| 安新县| 韶关市| 揭阳市| 赤峰市| 密山市| 禄劝| 泌阳县| 德化县| 白城市| 鲜城| 永宁县| 上栗县| 库车县| 盱眙县| 华亭县| 嵊州市| 武宣县| 阳城县| 阳江市| 资阳市| 札达县| 双鸭山市| 永州市| 长沙县| 邵阳市| 威信县| 台东市| 台山市| 佛山市| 东莞市| 鄱阳县| 河北区| 会宁县|