├── AndroidManifest.xml
├── activity_main.xml
└── MainActivity.java
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
18 |
19 |
27 |
28 |
--------------------------------------------------------------------------------
/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.technical.myapplication;
2 |
3 |
4 | import androidx.annotation.NonNull;
5 | import androidx.annotation.Nullable;
6 | import androidx.appcompat.app.AppCompatActivity;
7 | import androidx.core.app.ActivityCompat;
8 |
9 |
10 | import android.Manifest;
11 | import android.app.Activity;
12 | import android.content.Context;
13 | import android.content.Intent;
14 | import android.content.IntentSender;
15 | import android.content.pm.PackageManager;
16 | import android.location.LocationManager;
17 | import android.os.Build;
18 | import android.os.Bundle;
19 |
20 | import android.os.Looper;
21 | import android.view.View;
22 | import android.widget.Button;
23 | import android.widget.TextView;
24 | import android.widget.Toast;
25 |
26 | import com.google.android.gms.common.api.ApiException;
27 | import com.google.android.gms.common.api.ResolvableApiException;
28 | import com.google.android.gms.location.LocationCallback;
29 | import com.google.android.gms.location.LocationRequest;
30 | import com.google.android.gms.location.LocationResult;
31 | import com.google.android.gms.location.LocationServices;
32 | import com.google.android.gms.location.LocationSettingsRequest;
33 | import com.google.android.gms.location.LocationSettingsResponse;
34 | import com.google.android.gms.location.LocationSettingsStatusCodes;
35 | import com.google.android.gms.tasks.OnCompleteListener;
36 | import com.google.android.gms.tasks.Task;
37 |
38 |
39 | public class MainActivity extends AppCompatActivity {
40 |
41 | private TextView AddressText;
42 | private Button LocationButton;
43 | private LocationRequest locationRequest;
44 |
45 |
46 | @Override
47 | protected void onCreate(Bundle savedInstanceState) {
48 | super.onCreate(savedInstanceState);
49 | setContentView(R.layout.activity_main);
50 |
51 | AddressText = findViewById(R.id.addressText);
52 | LocationButton = findViewById(R.id.locationButton);
53 |
54 | locationRequest = LocationRequest.create();
55 | locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
56 | locationRequest.setInterval(5000);
57 | locationRequest.setFastestInterval(2000);
58 |
59 | LocationButton.setOnClickListener(new View.OnClickListener() {
60 | @Override
61 | public void onClick(View v) {
62 |
63 | getCurrentLocation();
64 | }
65 | });
66 |
67 |
68 | }
69 |
70 | @Override
71 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
72 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
73 |
74 | if (requestCode == 1){
75 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
76 |
77 | if (isGPSEnabled()) {
78 |
79 | getCurrentLocation();
80 |
81 | }else {
82 |
83 | turnOnGPS();
84 | }
85 | }
86 | }
87 |
88 |
89 | }
90 |
91 | @Override
92 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
93 | super.onActivityResult(requestCode, resultCode, data);
94 |
95 | if (requestCode == 2) {
96 | if (resultCode == Activity.RESULT_OK) {
97 |
98 | getCurrentLocation();
99 | }
100 | }
101 | }
102 |
103 | private void getCurrentLocation() {
104 |
105 |
106 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
107 | if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
108 |
109 | if (isGPSEnabled()) {
110 |
111 | LocationServices.getFusedLocationProviderClient(MainActivity.this)
112 | .requestLocationUpdates(locationRequest, new LocationCallback() {
113 | @Override
114 | public void onLocationResult(@NonNull LocationResult locationResult) {
115 | super.onLocationResult(locationResult);
116 |
117 | LocationServices.getFusedLocationProviderClient(MainActivity.this)
118 | .removeLocationUpdates(this);
119 |
120 | if (locationResult != null && locationResult.getLocations().size() >0){
121 |
122 | int index = locationResult.getLocations().size() - 1;
123 | double latitude = locationResult.getLocations().get(index).getLatitude();
124 | double longitude = locationResult.getLocations().get(index).getLongitude();
125 |
126 | AddressText.setText("Latitude: "+ latitude + "\n" + "Longitude: "+ longitude);
127 | }
128 | }
129 | }, Looper.getMainLooper());
130 |
131 | } else {
132 | turnOnGPS();
133 | }
134 |
135 | } else {
136 | requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
137 | }
138 | }
139 | }
140 |
141 | private void turnOnGPS() {
142 |
143 |
144 |
145 | LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
146 | .addLocationRequest(locationRequest);
147 | builder.setAlwaysShow(true);
148 |
149 | Task result = LocationServices.getSettingsClient(getApplicationContext())
150 | .checkLocationSettings(builder.build());
151 |
152 | result.addOnCompleteListener(new OnCompleteListener() {
153 | @Override
154 | public void onComplete(@NonNull Task task) {
155 |
156 | try {
157 | LocationSettingsResponse response = task.getResult(ApiException.class);
158 | Toast.makeText(MainActivity.this, "GPS is already tured on", Toast.LENGTH_SHORT).show();
159 |
160 | } catch (ApiException e) {
161 |
162 | switch (e.getStatusCode()) {
163 | case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
164 |
165 | try {
166 | ResolvableApiException resolvableApiException = (ResolvableApiException) e;
167 | resolvableApiException.startResolutionForResult(MainActivity.this, 2);
168 | } catch (IntentSender.SendIntentException ex) {
169 | ex.printStackTrace();
170 | }
171 | break;
172 |
173 | case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
174 | //Device does not have location
175 | break;
176 | }
177 | }
178 | }
179 | });
180 |
181 | }
182 |
183 | private boolean isGPSEnabled() {
184 | LocationManager locationManager = null;
185 | boolean isEnabled = false;
186 |
187 | if (locationManager == null) {
188 | locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
189 | }
190 |
191 | isEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
192 | return isEnabled;
193 |
194 | }
195 |
196 |
197 | }
--------------------------------------------------------------------------------