Quantcast
Channel: vimviv.com knowledge base » Android
Viewing all articles
Browse latest Browse all 10

Prompt user to enable location providers in Android

$
0
0

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.
location providers

3. If location providers are not enabled, ask user to enable location providers and launch location settings activity.
Prompt user
4. Using onActivityResult check user has changed location settings or not.
Activity result
5. Dont forget to add user permission to AndroidMenifest.xml file.

 

Incoming search terms:


Viewing all articles
Browse latest Browse all 10

Trending Articles