Enable and disable wifi and mobile data links

Source: Internet
Author: User

Some time ago, there was a need in the company to control the user's network connection mode. At that time, wifi was well controlled, but the gprs mobile data seemed to have no APIs.

When I checked the results, there was really no api for you to control, so I had to find a solution myself. At that time, I thought that there was such a switch in the settings.

Then, the source code of settings is opened.

As a result, I found out how to close the mobile data link in the source code.

In fact, the ConnectivityManager class controls our mobile data, but it does not open or close this method publicly, so we need to call this method through reflection.

Okay. Let's just look at the effect.

Below is the code

Package com. xiaobin. test; import java. lang. reflect. method; import android. app. activity; import android. content. context; import android.net. connectivityManager; import android.net. networkInfo; import android.net. wifi. wifiManager; import android. OS. bundle; import android. telephony. telephonyManager; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. Button; public class MainActivity extends Activity implements OnClickListener {private Button bt_open_wifi; private Button listener; private WifiManager wifiManager; private TelephonyManager telephonyManager; private ConnectivityManager connectivityManager; // check whether there is a network connection. If there is a network connection, the value is true. If not, the value is falseprivate boolean isConnect = false; private Meth. Od method; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // get a wifi manager to manage the wifi switch wifiManager = (WifiManager) getSystemService (Context. WIFI_SERVICE); // get a telphonyManager, which is used to determine whether we have a networked telephonyManager = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE); if (telephonyManager. getDataState () = Telep HonyManager. DATA_CONNECTED) {isConnect = true; System. out. println (isConnect);} // get a link manager, which controls connectivityManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager. getActiveNetworkInfo (); if (networkInfo = null) {System. out. println ("No network available currently");} else {System. out. println (networkInfo. isAvailable (); System. out. println (networkInf O. getTypeName ();} try {// This is a hidden method in ConnectivityManager, it can be used to enable or disable mobile data, that is, gprs. // here, it is worth noting that the parameter it passes is a boolean value, so this class is boolean. class or Boolean. TYPE // previously, Boolean was used. it took a long time to find the class result. This is the wrong method = ConnectivityManager. class. getDeclaredMethod ("setMobileDataEnabled", Boolean. TYPE);} catch (Exception e) {e. printStackTrace ();} bt_open_wifi = (Button) findViewById (R. id. bt_open_wifi); bt_cl Ose_wifi = (Button) findViewById (R. id. bt_close_wifi); bt_open_gprs = (Button) findViewById (R. id. bt_open_gprs); bt_close_gprs = (Button) findViewById (R. id. bt_close_gprs); terminate (this); bt_open_upls.setonclicklistener (this); terminate (this) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). infla Te (R. menu. main, menu); return true ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. bt_open_wifi: if (! WifiManager. isWifiEnabled () {wifiManager. setWifiEnabled (true); System. out. println ("enabling wifi");} break; case R. id. bt_close_wifi: if (wifiManager. isWifiEnabled () {wifiManager. setWifiEnabled (false); System. out. println ("disabling wifi");} break; case R. id. bt_open_gprs: if (! IsConnect) {try {method. invoke (connectivityManager, true); System. out. println ("enabling gprs"); isConnect = true;} catch (Exception e) {e. printStackTrace () ;}} break; case R. id. bt_close_gprs: if (isConnect) {try {method. invoke (connectivityManager, false); System. out. println ("shutting down gprs"); isConnect = false;} catch (Exception e) {e. printStackTrace () ;}} break; default: break ;}}}


In fact, the above Code is also very simple, and the main thing is a hidden function setMobileDataEnabled. Just call it through reflection.

Of course, you need to add the corresponding permissions.

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

There are a lot of comments above. If you have any questions, you can leave a message.


Well, this is the time to come. Next, I will write down the technical difficulties I encountered at work one after another. I will study with you.


Source code download


Zookeeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.