Code example for LocationManager: Finding current location

Methods: getBestProvidergetLastKnownLocationisProviderEnabled

0
	        criteria.setAltitudeRequired(false);
	        criteria.setBearingRequired(false);
	        criteria.setCostAllowed(true);
	        criteria.setPowerRequirement(Criteria.POWER_LOW); // µÍ¹¦ºÄ
	    	 
	        String provider = locationManager.getBestProvider(criteria, true); // »ñÈ¡GPSÐÅÏ¢
	         
	        Location location = null;
	       if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
	    	   location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // ͨ¹ýGPS»ñȡλÖÃ
	    	   //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
	    	    
	    	   if(location == null){
	    		   if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
	   	        		location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // ͨ¹ýNETWORK»ñȡλÖÃ
	   	        		//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
	   	        	} 
	    	   } 
	    	   showLocation(location);
	        } 
	        else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
	        	location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // ͨ¹ýNETWORK»ñȡλÖÃ
	        	//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
	        	showLocation(location);
	        } 
	        else{ 
	        	 Toast toast = Toast.makeText(getApplicationContext(),"NO SERVICE ENABLED", Toast.LENGTH_LONG);
Experience pair programming with AI  Get Codota for Java