Open and closed code implementations for WiFi and mobile data (2G/3G)

Source: Internet
Author: User

Mainactivity as follows:
Package Cc.vv;import Java.lang.reflect.method;import Android.net.connectivitymanager;import Android.net.wifi.wifimanager;import Android.os.bundle;import Android.telephony.telephonymanager;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.app.activity;import android.content.context;/** * Demo Description: * 1 turn WiFi on and off * 2 turn device mobile data on and off (2g/3g) * * * Note permissions: * &L T;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"/> * * Reference: * 1 http://blog.csdn.net/u011485531/article/details/ 18016351 * 2 http://blog.csdn.net/li_star/article/details/38758453 * Thank you very much */public class mainactivity ext Ends Activity {private Button Mopenwifibutton;    Private Button Mclosewifibutton;    Private Button Mopenmobiledatabutton;    Private Button Mclosemobiledatabutton;    Private Context Mcontext;    Private Clicklistenerimpl Mclicklistenerimpl;    Private Wifimanager Mwifimanager;    Private Telephonymanager Mtelephonymanager;    Private Connectivitymanager Mconnectivitymanager;    Private Boolean ismobiledataenabled=false; Private Method Mmethod; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main); init ();} private void Init () {mcontext=this;mclicklistenerimpl=new clicklistenerimpl (); mwifimanager= (Wifimanager) Mcontext.getsystemservice (Context.wifi_service); mconnectivitymanager= (Connectivitymanager) Mcontext.getsystemservice (Context.connectivity_service); mtelephonymanager= (Telephonymanager) Mcontext.getsystemservice (Context.telephony_service);//Determine if the current phone is using Mobiledata (mobile data) if ( Mtelephonymanager.getdatastate () ==telephonymanager.data_connected) {ismobiledataenabled=true;} mopenwifibutton= (Button) Findviewbyid (R.id.openwifibutton); Mopenwifibutton.setonclicklistener ( Mclicklistenerimpl); mclosewifibutton= (Button) Findviewbyid (R.id.closewifibutton); Mclosewifibutton.setonclicklistener (Mclicklistenerimpl); mopenmobiledatabutton= (Button) findViewById ( R.id.openmobiledatabutton); Mopenmobiledatabutton.setonclicklistener (Mclicklistenerimpl); MCloseMobileDataButton = (Button) Findviewbyid (R.id.closemobiledatabutton); Mclosemobiledatabutton.setonclicklistener (MClickListenerImpl );} Private class Clicklistenerimpl implements Onclicklistener {@Overridepublic void OnClick (View v) {switch (V.getid ()) {CAs e R.id.openwifibutton:openwifi (); break;case R.id.closewifibutton:closewifi (); Break;case R.id.openMobileDataButton : Openmobiledata (); break;case r.id.closemobiledatabutton:closemobiledata (); break;default:break;}}} Open wifiprivate void Openwifi () {if (!mwifimanager.iswifienabled ()) {mwifimanager.setwifienabled (true);}} Close Wifiprivate void Closewifi () {if (mwifimanager.iswifienabled ()) {mwifimanager.setwifienabled (false);}} Open move Data private void Openmobiledata () {if (!ismobiledataenabled) {try {mmethod= ConnectivityManager.class.getDeclaredMethod ("setmobiledataenabled", Boolean.type); Mmethod.invoke ( Mconnectivitymanager, true); ismobiledataenabled=true;} catch (Exception e) {}}}//Close move data private void Closemobiledata () {if (ismobiledataenabled) {try {mmethod=connectivity Manager.class.getDeclaredMethod ("setmobiledataenabled", Boolean.type); Mmethod.invoke (Mconnectivitymanager, False ); ismobiledataenabled=false;} catch (Exception e) {}}}}

Main.xml as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "> < Button android:id= "@+id/openwifibutton" android:layout_width= "wrap_content" android:layout_height= "WR Ap_content "android:layout_centerhorizontal=" true "android:layout_margintop=" 30dip "android:text=" Open W Ifi "/> <button android:id=" @+id/closewifibutton "android:layout_width=" Wrap_content "a ndroid:layout_height= "Wrap_content" android:layout_centerhorizontal= "true" android:layout_below= "@id/openwif IButton "android:layout_margintop=" 30dip "android:text=" off WiFi "/> <button android:id        = "@+id/openmobiledatabutton" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"      Android:layout_centerhorizontal= "true"  android:layout_below= "@id/closewifibutton" android:layout_margintop= "30dip" android:text= "Open mobile data"/> <button android:id= "@+id/closemobiledatabutton" android:layout_width= "Wrap_content" Android oid:layout_height= "Wrap_content" android:layout_centerhorizontal= "true" android:layout_below= "@id/openmobile Databutton "android:layout_margintop=" 30dip "android:text=" Turn off mobile data "/></relativelayout>


Open and closed code implementations for WiFi and mobile data (2G/3G)

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.