How To Start Service Again When App Restarts And User Already Granted Location Access
I am able to implement a simple gpsTracker using a service that runs on background like this: public class MyService extends Service { private static final String TAG = 'BOOMBO
Solution 1:
Well You have to start the service in two cases:-
1) When user grant the location access.
2)When user already granted the permission after launching application again.
So you have to handle both the ways
Try this.
publicstatic final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean 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 thread waiting for the user's response! After the user// sees the explanation, try again to request the permission.new AlertDialog.Builder(this)
.setTitle(R.string.title_location_permission)
.setMessage(R.string.text_location_permission)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
publicvoidonClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
})
.create()
.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission. ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
returnfalse;
} else {
returntrue;
}
}
@Override
publicvoidonRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the// location-related task you need to do.if (ContextCompat.checkSelfPermission(this,
Manifest.permission. ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//start your service here
startService(new Intent(this, MyService.class));
}
} else {
// permission denied, boo! Disable the// functionality that depends on this permission.
}
return;
}
}
}
The in OnResume()
do this
@OverrideprotectedvoidonResume() {
super.onResume();
if (checkLocationPermission()) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission. ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//start your service herestartService(newIntent(this, MyService.class));
}
}
}
No need to Call this Method in onCreate()
publicvoidinitService(View view){
// no need to initialize permission and service class here
}
Post a Comment for "How To Start Service Again When App Restarts And User Already Granted Location Access"