Android網路開啟、關閉整理

來源:互聯網
上載者:User

標籤:android   c   class   blog   java   a   

近段時間由於要對行動電話通訊狀況進行判斷、開啟和關閉,從網上找了些資料,現整理如下


包含了對WiFi、GPRS、飛航模式的開啟、關閉以及一些狀態的檢測,在小米和三星平板上測試均通過

[java] view plaincopy
    1. package com.my.device_admin.business;  
    2.   
    3. import java.lang.reflect.Method;  
    4.   
    5. import android.content.Context;  
    6. import android.content.Intent;  
    7. import android.net.ConnectivityManager;  
    8. import android.net.NetworkInfo;  
    9. import android.net.wifi.WifiManager;  
    10. import android.provider.Settings;  
    11.   
    12. public class NetworkManager {  
    13.   
    14.     private Context context;  
    15.     private ConnectivityManager connManager;  
    16.   
    17.     public NetworkManager(Context context) {  
    18.         this.context = context;  
    19.         connManager = (ConnectivityManager) this.context  
    20.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
    21.     }  
    22.   
    23.     /** 
    24.      * @return 網路是否串連可用 
    25.      */  
    26.     public boolean isNetworkConnected() {  
    27.   
    28.         NetworkInfo networkinfo = connManager.getActiveNetworkInfo();  
    29.   
    30.         if (networkinfo != null) {  
    31.             return networkinfo.isConnected();  
    32.         }  
    33.   
    34.         return false;  
    35.     }  
    36.   
    37.     /** 
    38.      * @return wifi是否串連可用 
    39.      */  
    40.     public boolean isWifiConnected() {  
    41.   
    42.         NetworkInfo mWifi = connManager  
    43.                 .getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
    44.   
    45.         if (mWifi != null) {  
    46.             return mWifi.isConnected();  
    47.         }  
    48.   
    49.         return false;  
    50.     }  
    51.   
    52.     /** 
    53.      * 當wifi不能訪問網路時,mobile才會起作用 
    54.      * @return GPRS是否串連可用 
    55.      */  
    56.     public boolean isMobileConnected() {  
    57.   
    58.         NetworkInfo mMobile = connManager  
    59.                 .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
    60.   
    61.         if (mMobile != null) {  
    62.             return mMobile.isConnected();  
    63.         }  
    64.         return false;  
    65.     }  
    66.   
    67.     /** 
    68.      * GPRS網路開關 反射ConnectivityManager中hide的方法setMobileDataEnabled 可以開啟和關閉GPRS網路 
    69.      *  
    70.      * @param isEnable 
    71.      * @throws Exception 
    72.      */  
    73.     public void toggleGprs(boolean isEnable) throws Exception {  
    74.         Class<?> cmClass = connManager.getClass();  
    75.         Class<?>[] argClasses = new Class[1];  
    76.         argClasses[0] = boolean.class;  
    77.   
    78.         // 反射ConnectivityManager中hide的方法setMobileDataEnabled,可以開啟和關閉GPRS網路  
    79.         Method method = cmClass.getMethod("setMobileDataEnabled", argClasses);  
    80.         method.invoke(connManager, isEnable);  
    81.     }  
    82.   
    83.     /** 
    84.      * WIFI網路開關 
    85.      *  
    86.      * @param enabled 
    87.      * @return 設定是否success 
    88.      */  
    89.     public boolean toggleWiFi(boolean enabled) {  
    90.         WifiManager wm = (WifiManager) context  
    91.                 .getSystemService(Context.WIFI_SERVICE);  
    92.         return wm.setWifiEnabled(enabled);  
    93.   
    94.     }  
    95.       
    96.     /** 
    97.      *  
    98.      * @return 是否處于飛行模式 
    99.      */  
    100.     public boolean isAirplaneModeOn() {    
    101.         // 傳回值是1時表示處于飛行模式    
    102.         int modeIdx = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);    
    103.         boolean isEnabled = (modeIdx == 1);  
    104.         return isEnabled;  
    105.     }    
    106.     /** 
    107.      * 飛航模式開關 
    108.      * @param setAirPlane 
    109.      */  
    110.     public void toggleAirplaneMode(boolean setAirPlane) {    
    111.         Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);    
    112.         // 廣播飛航模式訊號的改變,讓相應的程式可以處理。    
    113.         // 不發送廣播時,在非飛航模式下,Android 2.2.1上測試關閉了Wifi,不關閉正常的通話網路(如GMS/GPRS等)。    
    114.         // 不發送廣播時,在飛航模式下,Android 2.2.1上測試無法關閉飛航模式。    
    115.         Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);    
    116.         // intent.putExtra("Sponsor", "Sodino");    
    117.         // 2.3及以後,需設定此狀態,否則會一直處於與電訊廠商斷連的情況    
    118.         intent.putExtra("state", setAirPlane);    
    119.         context.sendBroadcast(intent);    
    120.     }  
    121. } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.