Android 監(jiān)聽WiFi的開關(guān)狀態(tài)實(shí)現(xiàn)代碼
Android 監(jiān)聽WiFi的開關(guān)狀態(tài)實(shí)現(xiàn)代碼
WifiSwitch_Presenter 源碼:
package com.yiba.wifi.sdk.lib.presenter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
/**
* Created by ${zhaoyanjun} on 2017/3/29.
* Wifi 開關(guān)監(jiān)聽
*/
public class WifiSwitch_Presenter {
private Context mContext ;
private Receiver receiver ;
private WifiSwitch_Interface mInterface ;
public WifiSwitch_Presenter( Context context , WifiSwitch_Interface mInterface ){
this.mContext = context ;
this.mInterface = mInterface ;
observeWifiSwitch();
}
private void observeWifiSwitch(){
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
receiver = new Receiver() ;
mContext.registerReceiver(receiver, filter);
}
/**
* 釋放資源
*/
public void onDestroy(){
if ( receiver != null ){
mContext.unregisterReceiver( receiver );
}
if (mContext!=null){
mContext = null;
}
}
class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLED:
if (mInterface != null){
mInterface.wifiSwitchState(WifiSwitch_Interface.WIFI_STATE_DISABLED);
}
break;
case WifiManager.WIFI_STATE_DISABLING:
if (mInterface != null){
mInterface.wifiSwitchState(WifiSwitch_Interface.WIFI_STATE_DISABLING);
}
break;
case WifiManager.WIFI_STATE_ENABLED:
if (mInterface != null){
mInterface.wifiSwitchState(WifiSwitch_Interface.WIFI_STATE_ENABLED);
}
break;
case WifiManager.WIFI_STATE_ENABLING:
if ( mInterface != null ) {
mInterface.wifiSwitchState(WifiSwitch_Interface.WIFI_STATE_ENABLING);
}
break;
case WifiManager.WIFI_STATE_UNKNOWN:
if ( mInterface != null ){
mInterface.wifiSwitchState( WifiSwitch_Interface.WIFI_STATE_UNKNOWN );
}
break;
}
}
}
}
WifiSwitch_Interface 源碼
package com.yiba.wifi.sdk.lib.presenter;
/**
* Created by ${zhaoyanjun} on 2017/3/29.
* Wifi 開關(guān)監(jiān)聽
*/
public interface WifiSwitch_Interface {
int WIFI_STATE_ENABLING = 0 ;
int WIFI_STATE_ENABLED = 1 ;
int WIFI_STATE_DISABLING = 2 ;
int WIFI_STATE_DISABLED = 3 ;
int WIFI_STATE_UNKNOWN = 4 ;
void wifiSwitchState( int state );
}
使用方式 MainActivity :
package com.yiba.core;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements WifiSwitch_Interface {
private WifiSwitch_Presenter wifiSwitch_presenter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiSwitch_presenter = new WifiSwitch_Presenter( this , this ) ;
}
@Override
public void wifiSwitchState(int state) {
switch ( state ){
case WifiSwitch_Interface.WIFI_STATE_DISABLED :
Toast.makeText(this, "WiFi 已經(jīng)關(guān)閉", Toast.LENGTH_SHORT).show();
break;
case WifiSwitch_Interface.WIFI_STATE_DISABLING:
Toast.makeText(this, "WiFi 正在關(guān)閉", Toast.LENGTH_SHORT).show();
break;
case WifiSwitch_Interface.WIFI_STATE_ENABLED :
Toast.makeText(this, "WiFi 已經(jīng)打開", Toast.LENGTH_SHORT).show();
break;
case WifiSwitch_Interface.WIFI_STATE_ENABLING :
Toast.makeText(this, "WiFi 正在打開", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//釋放資源
if ( wifiSwitch_presenter != null ){
wifiSwitch_presenter.onDestroy();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android游戲開發(fā) 自定義手勢--輸入法手勢技術(shù)
本文主要介紹 Android游戲開發(fā)中自定義手勢--輸入法手勢技術(shù),這里提供了實(shí)現(xiàn)效果圖以及示例代碼,有開發(fā)手機(jī)游戲的朋友可以參考下2016-08-08
Android通過自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼
這篇文章主要介紹了Android通過自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android實(shí)現(xiàn)價(jià)格走勢自定義曲線圖
本篇文章主要介紹了Android實(shí)現(xiàn)價(jià)格走勢曲線圖,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04
Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(一)
這篇文章主要介紹了Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(一) 的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android RecyclerView添加FootView和HeadView
這篇文章主要介紹了Android RecyclerView添加FootView和HeadView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
取消Android Studio項(xiàng)目與SVN關(guān)聯(lián)的方法
今天小編就為大家分享一篇關(guān)于取消Android Studio項(xiàng)目與SVN關(guān)聯(lián)的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android藍(lán)牙的開啟和搜索設(shè)備功能開發(fā)實(shí)例
這篇文章主要介紹了Android藍(lán)牙服務(wù)啟動(dòng)搜索流程,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的2023-04-04
Android中實(shí)現(xiàn)EditText圓角的方法
Android中實(shí)現(xiàn)EditText圓角的方法,需要的朋友可以參考一下2013-03-03
Android利用ContentProvider獲取本地?cái)?shù)據(jù)的方法
這篇文章主要介紹了Android利用ContentProvider獲取本地?cái)?shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04

