Android網路開啟、關閉整理

來源:互聯網
上載者:User

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

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

package com.my.device_admin.business;import java.lang.reflect.Method;import android.content.Context;import android.content.Intent;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.net.wifi.WifiManager;import android.provider.Settings;public class NetworkManager {private Context context;private ConnectivityManager connManager;public NetworkManager(Context context) {this.context = context;connManager = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);}/** * @return 網路是否串連可用 */public boolean isNetworkConnected() {NetworkInfo networkinfo = connManager.getActiveNetworkInfo();if (networkinfo != null) {return networkinfo.isConnected();}return false;}/** * @return wifi是否串連可用 */public boolean isWifiConnected() {NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);if (mWifi != null) {return mWifi.isConnected();}return false;}/** * 當wifi不能訪問網路時,mobile才會起作用 * @return GPRS是否串連可用 */public boolean isMobileConnected() {NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);if (mMobile != null) {return mMobile.isConnected();}return false;}/** * GPRS網路開關 反射ConnectivityManager中hide的方法setMobileDataEnabled 可以開啟和關閉GPRS網路 *  * @param isEnable * @throws Exception */public void toggleGprs(boolean isEnable) throws Exception {Class<?> cmClass = connManager.getClass();Class<?>[] argClasses = new Class[1];argClasses[0] = boolean.class;// 反射ConnectivityManager中hide的方法setMobileDataEnabled,可以開啟和關閉GPRS網路Method method = cmClass.getMethod("setMobileDataEnabled", argClasses);method.invoke(connManager, isEnable);}/** * WIFI網路開關 *  * @param enabled * @return 設定是否success */public boolean toggleWiFi(boolean enabled) {WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);return wm.setWifiEnabled(enabled);}    /**     *      * @return 是否處于飛行模式     */    public boolean isAirplaneModeOn() {          // 傳回值是1時表示處于飛行模式          int modeIdx = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);          boolean isEnabled = (modeIdx == 1);        return isEnabled;    }      /**     * 飛航模式開關     * @param setAirPlane     */    public void toggleAirplaneMode(boolean setAirPlane) {          Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);          // 廣播飛航模式訊號的改變,讓相應的程式可以處理。          // 不發送廣播時,在非飛航模式下,Android 2.2.1上測試關閉了Wifi,不關閉正常的通話網路(如GMS/GPRS等)。          // 不發送廣播時,在飛航模式下,Android 2.2.1上測試無法關閉飛航模式。          Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);          // intent.putExtra("Sponsor", "Sodino");          // 2.3及以後,需設定此狀態,否則會一直處於與電訊廠商斷連的情況          intent.putExtra("state", setAirPlane);          context.sendBroadcast(intent);      }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.