Now a days almost every smartphone applications use location based functionalities. But every articles say that disable wifi, 3g, gps etc for battery saving. So for every location based applications lots of problems are there like. What are location providers available in device? You can enable GPS using some method but best methods is ask user to enable location providers.
In this post we will see that how can we prompt user to enable location providers.
private void checkLocationProviders(){ //String provider = Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){ Toast.makeText(EnableLocationActivity.this, "GPS provider Enabled: ",Toast.LENGTH_LONG).show(); }else if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ Toast.makeText(EnableLocationActivity.this, "Network provider Enabled: ",Toast.LENGTH_LONG).show(); }else{ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Location providers are not available. Enable GPS or network providers.") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, 1); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EnableLocationActivity.this.finish(); } }).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { checkLocationProviders(); super.onActivityResult(requestCode, resultCode, data); }
Explanation of above code.
1. First we need to create LocationManager.
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
2. using isProviderEnabled Check location providers are already enabled or not.
3. If location providers are not enabled, ask user to enable location providers and launch location settings activity.
4. Using onActivityResult check user has changed location settings or not.
5. Dont forget to add user permission to AndroidMenifest.xml file.
Incoming search terms:
- how to enable location provider in android (19)
- enable location provider (13)
- action_location_source_settings (4)
- android prompt to enable gps (3)
- android prompt user to enable gps (2)
- how to enable locationmanager network_provider (2)
- android prompt the user to enable gps (2)
- android prompt settings (2)
- how to enable network provider android without user knowledge (2)
- network provider android (2)