Android determines if GPS is turned on and forces the user to open the GPS

Source: Internet
Author: User

Introduction: In our application to provide users with location services, often want to provide users with accurate point of location services, which requires the user to cooperate. We must first detect the user's mobile phone's GPS is currently open, if not open, pop-up dialog box prompt. If users do not cooperate with us, we can only use the base station positioning method. If our application must be opened by the user to use the GPS, then the rogue point of action is to force users to open the GPS.

Clarifying Concepts:

Location Services GPS: A global satellite positioning system, using a network of 24 satellites to triangulate the position of the receiver and provide latitude and longitude coordinates. While GPS provides excellent positional accuracy, the location of the positioning needs to be seen where a satellite or track is visible.

Location Services AGPS: Auxiliary global satellite Positioning System (English: Assisted Global Positioning System, abbreviation: AGPS) is a mode of operation of GPs. It can use the mobile phone base station information, with the traditional GPS satellite, so that positioning faster. In Chinese, it should be a network-aided GPS positioning system. Popularly said that aGPS is in the past through the satellite to accept the location of the signal and the mobile operation of the GSM or CDMA network station positioning information, on the one hand from the mobile phone with aGPS to obtain from the satellite positioning information, but also rely on the mobile phone through the GPRS network of China Mobile download Auxiliary positioning information, The two combine to complete the positioning. With the traditional GPS (Globalpositioningsystem Global Positioning System) first positioning to 2, 3 minutes compared to the aGPS first time to locate the fastest only a few seconds, while the AGPS also completely solve the general GPS equipment in the room can not obtain location information defects.

First, the detection of the user's mobile phone GPS is currently open, the code is as follows:

    /**          * To determine whether the GPS is on, GPS or aGPS to open a think is open          * @param context          * @return true to open */public          static final Boolean IsOPen (Final context context) {              Locationmanager Locationmanager                                        = (Locationmanager) Context.getsystemservice (context.location_service);              GPS satellite positioning, positioning level can be accurate to the street (through 24 satellite positioning, in outdoor and open location accurate, fast)              Boolean GPS = locationmanager.isproviderenabled ( Locationmanager.gps_provider);              A location determined by the WLAN or mobile network (3G/2G), also known as AGPS, assists with GPS positioning. Mainly used in indoor or covering (complex or dense deep forest, etc.) to locate in dense place)              Boolean network = locationmanager.isproviderenabled (locationmanager.network_ PROVIDER);              if (GPS | | network) {                  return true;              }                    return false;          }  
Second, the force to help users open the GPS, the code is as follows:
    /**          * Force the user to open the GPS          * @param context *          /public          static final void Opengps (context context) {              Intent Gpsintent = new Intent ();              Gpsintent.setclassname ("Com.android.settings",                      "Com.android.settings.widget.SettingsAppWidgetProvider");              Gpsintent.addcategory ("Android.intent.category.ALTERNATIVE");              Gpsintent.setdata (Uri.parse ("Custom:3"));              try {                  Pendingintent.getbroadcast (context, 0, gpsintent, 0). Send ();              } catch (Canceledexception e) {                  E.printstacktrace ();              }          }  
Third, in the Androidmanifest.xml file need to add the permissions:
    <uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/>      <uses-permission Android:name= "Android.permission.ACCESS_FINE_LOCATION"/>      <uses-permission android:name= " Android.permission.ACCESS_WIFI_STATE "/>      <uses-permission android:name=" android.permission.ACCESS_ Network_state "/>      <uses-permission android:name=" Android.permission.CHANGE_WIFI_STATE "/>      < Uses-permission android:name= "Android.permission.INTERNET"/>  
From: http://blog.csdn.net/android_ls/article/details/8605931

Android determines if GPS is turned on and forces the user to open the GPS

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.