Created
August 15, 2018 08:23
-
-
Save catvinhquang/ada890e877df5280d2a5fceaec041c5c to your computer and use it in GitHub Desktop.
Enable GPS in app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package catvinhquang.enablegps; | |
import android.Manifest; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.os.Looper; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.ActivityCompat; | |
import android.text.method.ScrollingMovementMethod; | |
import android.view.View; | |
import android.widget.TextView; | |
import com.google.android.gms.common.api.ResolvableApiException; | |
import com.google.android.gms.location.FusedLocationProviderClient; | |
import com.google.android.gms.location.LocationCallback; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationResult; | |
import com.google.android.gms.location.LocationServices; | |
import com.google.android.gms.location.LocationSettingsRequest; | |
import com.google.android.gms.location.LocationSettingsResponse; | |
import com.google.android.gms.location.SettingsClient; | |
import com.google.android.gms.tasks.OnFailureListener; | |
import com.google.android.gms.tasks.OnSuccessListener; | |
import com.google.android.gms.tasks.Task; | |
/** | |
* Created by quangcv on 15/08/2018. | |
*/ | |
public class MainActivity extends Activity { | |
static final int REQUEST_GPS = 100; | |
static final int REQUEST_PERMISSION_LOCATION = 101; | |
static final LocationRequest locationRequest; | |
static { | |
locationRequest = new LocationRequest(); | |
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
locationRequest.setInterval(1000); | |
locationRequest.setFastestInterval(1000); | |
locationRequest.setNumUpdates(1); | |
} | |
final StringBuilder consoleBuilder = new StringBuilder(); | |
TextView console; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
console = new TextView(this); | |
console.setMovementMethod(new ScrollingMovementMethod()); | |
console.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
requestGPS(); | |
} | |
}); | |
console.setOnLongClickListener(new View.OnLongClickListener() { | |
@Override | |
public boolean onLongClick(View v) { | |
clear(); | |
return true; | |
} | |
}); | |
clear(); | |
setContentView(console); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == REQUEST_GPS) { | |
if (resultCode == RESULT_OK) { | |
log("GPS enabled"); | |
requestLocation(); | |
} else { | |
log("Request GPS is canceled"); | |
} | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
if (requestCode == REQUEST_PERMISSION_LOCATION) { | |
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { | |
log("Permission granted"); | |
requestLocation(); | |
} else { | |
log("Permission denied"); | |
} | |
} | |
} | |
void requestGPS() { | |
LocationSettingsRequest settingsRequest = new LocationSettingsRequest.Builder() | |
.addLocationRequest(locationRequest) | |
.setAlwaysShow(true) | |
.build(); | |
SettingsClient settingsClient = LocationServices.getSettingsClient(this); | |
Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(settingsRequest); | |
task.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() { | |
@Override | |
public void onSuccess(LocationSettingsResponse locationSettingsResponse) { | |
log("GPS enabled"); | |
requestLocation(); | |
} | |
}); | |
task.addOnFailureListener(this, new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception e) { | |
log("GPS disabled"); | |
if (e instanceof ResolvableApiException) { | |
try { | |
log("Requesting GPS"); | |
ResolvableApiException resolvable = (ResolvableApiException) e; | |
resolvable.startResolutionForResult(MainActivity.this, REQUEST_GPS); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
} | |
} | |
}); | |
} | |
void requestLocation() { | |
log("Requesting location"); | |
FusedLocationProviderClient providerClient = LocationServices.getFusedLocationProviderClient(this); | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
log("Request location is canceled"); | |
log("Request permission"); | |
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_LOCATION); | |
return; | |
} | |
providerClient.requestLocationUpdates(locationRequest, new LocationCallback() { | |
@Override | |
public void onLocationResult(LocationResult locationResult) { | |
Location location = locationResult.getLastLocation(); | |
log(location.getProvider() + " | " + location.getLatitude() + ", " + location.getLongitude()); | |
} | |
}, Looper.myLooper()); | |
} | |
void log(String message) { | |
consoleBuilder.append(message).append("\n"); | |
console.setText(consoleBuilder); | |
} | |
void clear() { | |
consoleBuilder.delete(0, consoleBuilder.length()); | |
consoleBuilder.append("Touch to continue, please!\nAnd long touch to clear :)\n"); | |
console.setText(consoleBuilder); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependency:
compile 'com.google.android.gms:play-services-location:15.0.1'