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:

/*** Determine if the GPS is on, GPS or aGPS to open a thought is open *@paramContext *@returnTrue indicates that the open*/      Public Static Final BooleanIsOPen (Finalcontext 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 the outdoor and open location accurate, fast)        BooleanGPS =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 in the shelter (complex or dense deep forest, etc.) dense place positioning)        BooleanNetwork =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 *@paramContext*/      Public Static Final voidOpengps (Context context) {Intent gpsintent=NewIntent (); Gpsintent.setclassname ("",                 ""); 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 "/>

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

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: 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.