Android Network on, off finishing

Source: Internet
Author: User
Tags samsung tablet

In recent time due to the mobile phone network status to judge, open and close, from the Internet to find some information, now organized as follows


Includes WiFi, GPRS, flight mode on, off, and some status detection, on the Xiaomi and Samsung tablet test pass

[Java]View Plaincopy
    1. Package  com.my.device_admin.business;
    2. Import  Java.lang.reflect.Method;
    3. Import  Android.content.Context;
    4. Import  android.content.Intent;
    5. Import  Android.net.ConnectivityManager;
    6. Import  Android.net.NetworkInfo;
    7. Import  Android.net.wifi.WifiManager;
    8. Import  android.provider.Settings;
    9. Public class NetworkManager {
    10. Private  context Context;
    11. Private  Connectivitymanager Connmanager;
    12. Public NetworkManager (Context context) {
    13. This  . Context = context;
    14. Connmanager = (Connectivitymanager) this. Context
    15. . Getsystemservice (Context.connectivity_service);
    16. }
    17. /**
    18. * @return network connectivity is available
    19.      */   
    20. Public boolean isnetworkconnected () {
    21. Networkinfo networkinfo = Connmanager.getactivenetworkinfo ();
    22. if (networkinfo! = null) {
    23. return  networkinfo.isconnected ();
    24. }
    25. return   false;
    26. }
    27. /**
    28. * @return WiFi connection is available
    29.      */   
    30. Public boolean iswificonnected () {
    31. Networkinfo Mwifi = Connmanager
    32. . Getnetworkinfo (Connectivitymanager.type_wifi);
    33. if (Mwifi! = null) {
    34. return  mwifi.isconnected ();
    35. }
    36. return   false;
    37. }
    38. /**
    39. * Mobile only works when WiFi cannot access the network
    40. * @return Whether GPRS connection is available
    41.      */   
    42. Public boolean ismobileconnected () {
    43. Networkinfo Mmobile = Connmanager
    44. . Getnetworkinfo (Connectivitymanager.type_mobile);
    45. if (mmobile! = null) {
    46. return  mmobile.isconnected ();
    47. }
    48. return   false;
    49. }
    50. /**
    51. * GPRS network switch Reflection Connectivitymanager Hide Method setmobiledataenabled can turn GPRS network on and off
    52.      * 
    53. * @param isenable
    54. * @throws Exception
    55.      */   
    56. Public  void Togglegprs (boolean isenable) throws Exception {
    57. class<?> Cmclass = Connmanager.getclass ();
    58. class<?>[] argclasses = new class[1];
    59. argclasses[0] = boolean. class  ;
    60. //Reflection Connectivitymanager in Hide method setmobiledataenabled, can turn GPRS network on and off   
    61. method = Cmclass.getmethod ("setmobiledataenabled", argclasses);
    62. Method.invoke (Connmanager, isenable);
    63. }
    64. /**
    65. * WiFi network switch
    66.      * 
    67. * @param enabled
    68. * @return Set whether success
    69.      */   
    70. Public boolean Togglewifi (boolean enabled) {
    71. Wifimanager wm = (Wifimanager) context
    72. . Getsystemservice (Context.wifi_service);
    73. return  wm.setwifienabled (enabled);
    74. }
    75. /**
    76.      * 
    77. * @return is in airplane mode
    78.      */   
    79. Public boolean Isairplanemodeon () {
    80. //Return value is 1 indicates in airplane mode   
    81. int modeidx = Settings.System.getInt (Context.getcontentresolver (), Settings.System.AIRPLANE_MODE_ON, 0    );
    82. Boolean  isenabled = (Modeidx = = 1);
    83. return  isenabled;
    84. }
    85. /**
    86. * Flight mode switch
    87. * @param setairplane
    88.      */   
    89. Public void Toggleairplanemode (boolean setairplane) {
    90. Settings.System.putInt (Context.getcontentresolver (), Settings.System.AIRPLANE_MODE_ON, Setairplane? 1  :  0    );
    91. //Broadcast flight mode signal changes, so that the corresponding program can be processed.   
    92. //Do not send the broadcast, in non-flight mode, the Android 2.2.1 on the test turned off WiFi, do not turn off the normal call network (such as GMS/GPRS, etc.).   
    93. //Do not send the broadcast when in airplane mode, the Android 2.2.1 on the test cannot turn off airplane mode.   
    94. Intent Intent = new Intent (intent.action_airplane_mode_changed);
    95. //Intent.putextra ("Sponsor", "Sodino");   
    96. //2.3 and later, you need to set this state, otherwise it will remain in the case of the operator disconnected   
    97. Intent.putextra ("state", Setairplane);
    98. Context.sendbroadcast (Intent);
    99. }
    100. }

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.