2011-10-10 7 views
24

मुझे उपयोगकर्ता का स्थान प्राप्त करने की आवश्यकता है। मैं प्रकटGPS_PROVIDER और/या NETWORK_PROVIDER को सक्षम करने के लिए उपयोगकर्ता को कैसे संकेत दें?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

में डाल उपयोगकर्ता GPS_PROVIDER और/या NETWORK_PROVIDER सक्षम करने के लिए संकेत करने के लिए कैसे?

उत्तर

33

आप स्थान सेटिंग के लिए एक विकल्प इरादा शुरू कर सकते हैं।

Intent gpsOptionsIntent = new Intent( 
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
startActivity(gpsOptionsIntent); 
+1

जीपीएस लेकिन इस सेटिंग स्क्रीन जहां जब भी आप जाना में पाश का कारण होगा अगर परीक्षण करने के लिए वापस यह सेटिंग स्क्रीन पर ले जाएगा –

+0

यह एक लूप क्यों बनाता है? –

+1

मुझे लगता है कि यह इस बात पर निर्भर करता है कि आपने यह कोड कहां रखा है। यदि आप अपना ऐप फिर से शुरू करते हैं तो गतिविधि शुरू करते हैं, तो आप एक लूप में समाप्त हो सकते हैं। लेकिन यह कोड के कारण ही नहीं है। –

5

आप जीपीएस की जाँच के बाद

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS); 
     startActivity(intent); 

उपयोग कर सकते हैं।

पूरा स्रोत पाया जा सकता है HERE या आप का उल्लेख कर सकते SO ANSWER

+0

ACTION_LOCATION_SOURCE_SETTINGS को ACTION_SECURITY_SETTINGS में बदलें – Xenione

+0

किसी अन्य SO उत्तर के लिए उपयोगी लिंक! –

8

LOCATION_MODE हो जाएगा LOCATION_MODE_OFF अगर जीपीएस बंद है, जो वापस आ जाएगी मूल्य 0.

int off = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE); 
    if(off==0){ 
     Intent onGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(onGPS); 
    } 
1

LocationListner लागू: तो

@Override 
public void onLocationChanged(Location location) { 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) 
{ 
if (provider.equals(LocationManager.GPS_PROVIDER)) 
{ 
    showGPSDiabledDialog(); 
} 
}  

सेटिंग्स खोलने के लिए संवाद दिखाएं:

public void showGPSDiabledDialog() { 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("GPS Disabled"); 
builder.setMessage("Gps is disabled, in order to use the application properly you need to enable GPS of your device"); 
builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_ENABLE_REQUEST); 
    } 
}).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 

    } 
}); 
mGPSDialog = builder.create(); 
mGPSDialog.show(); 
} 

अपने OnCreate विधि कॉल में:

LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
{ 
    return; 
} 
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

और विधि onActivityResult ovveride उपयोगकर्ता सही ढंग से सक्रिय

@Override 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
if (requestCode == GPS_ENABLE_REQUEST) 
{ 
    if (mLocationManager == null) 
    { 
     mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    } 

    if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
    { 
     showGPSDiabledDialog(); 
    } 
} 
else 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
} 
} 
संबंधित मुद्दे

 संबंधित मुद्दे