Android Location Permissions With GPS disabled Enable
Hello Everyone, In this Tutorial I Will Show you how Can I Get Android Location Permission Granted. In Case There is a Common Problemes When User Allow Permission Granted but not Gps Enable. So how Can I Solve This Problemes Flow this Step
Step 1: AndroidManifest.xml file Permission add
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
Step 2: Android XML Design Simple Button like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/location_on_id"
android:text="location get"
android:layout_centerInParent="true"
android:gravity="center"
android:layout_margin="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>
Step 3: Android goto MainActivity Java File Write Some Code for Permission
private int REQUEST_PERMISSION_CODE_LOCATION=1;
private Button locationbutton;
locationbutton=findViewById(R.id.location_on_id);
locationbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(MainActivity.this, "permission already granted..", Toast.LENGTH_SHORT).show();
statusCheck();
}
else {
Toast.makeText(MainActivity.this, "permission need", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT>=23)
{
requestPermissions(new String []{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_PERMISSION_CODE_LOCATION);
}
}
}
});
public void statusCheck() {
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
buildAlertMessageNoGps();
}
else{
Toast.makeText(this, "futher not available", Toast.LENGTH_SHORT).show();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==1)
{
if (grantResults.length !=-1)
{
grantResults[0]=PackageManager.PERMISSION_GRANTED;
}
}
}
Step 4: Output Show Sample like
android 9 API-28 ui for permission
android 10 API-29 ui for permission
android 11 API-30 ui for permission
Step 5: Next you need to Gps Enable (Check Gps Enable or Disable)
Step 6: Next if You are Click Yes--------goto Access my location
Maybe this tutorial is Very Helful
ReplyDeletenice tutorial site.
ReplyDeleteThanks dear
Delete