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 com.example.locationdialogbox; | |
import androidx.fragment.app.FragmentActivity; | |
import android.content.IntentSender; | |
import android.os.Bundle; | |
import android.util.Log; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; |
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
private GoogleMap mMap; | |
private GoogleApiClient googleApiClient; | |
final static int REQUEST_LOCATION = 199; |
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
enableLoc(); |
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
private void enableLoc() { | |
if (googleApiClient == null) { | |
googleApiClient = new GoogleApiClient.Builder(MapsActivity.this) | |
.addApi(LocationServices.API) | |
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { | |
@Override | |
public void onConnected(Bundle bundle) { | |
} |
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
dependencies { | |
implementation fileTree(dir: 'libs', include: ['*.jar']) | |
implementation 'androidx.appcompat:appcompat:1.0.0' | |
testImplementation 'junit:junit:4.12' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' | |
//google maps | |
implementation 'com.google.android.gms:play-services-maps:16.0.0' | |
implementation 'com.google.android.gms:play-services-location:16.0.0' |
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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MapsActivity" > | |
<fragment | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:map="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" |
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
package="com.example.googlemapsms"> | |
// Add these permissions | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.SEND_SMS"/> |
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
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; | |
private void checkLocationPermission() { | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED) { | |
// Should we show an explanation? | |
if (ActivityCompat.shouldShowRequestPermissionRationale(this, | |
Manifest.permission.ACCESS_FINE_LOCATION)) { | |
// Show an explanation to the user *asynchronously* -- don't block |
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
// Declare before onCreate() | |
FusedLocationProviderClient mFusedLocationClient; | |
// Initialize inside onCreate() | |
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); |
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
@Override | |
public void onMapReady(GoogleMap googleMap) { | |
mGoogleMap = googleMap; | |
mLocationRequest = new LocationRequest(); | |
mLocationRequest.setInterval(120000); // two minute interval | |
mLocationRequest.setFastestInterval(120000); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); | |
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
OlderNewer