Skip to main content

 


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




Step 6: this Tutorial is done,thanks








Comments

Post a Comment

Popular posts from this blog

 var val  deference in kotlin  fun main () { var Android_Programing_language : String = "java" println ( "android official programing language: " + Android_Programing_language ) Android_Programing_language = "Kotline" println ( "2019 google announced android official programing language: " + Android_Programing_language ) val Android_MultiPlatform_language : String = "Dart" println ( "multiple device run : " + Android_MultiPlatform_language ) /* Android_MultiPlatform_language="" /* note: var--------------valu changable note: val--------------valu not changable (final) */ */ }
 Firebase Cloud Messaging With  push notification with Image  public class ImageDownload extends AsyncTask < String , Void , Bitmap >{ @Override protected Bitmap doInBackground ( String ... strings ) { InputStream inputStream ; try { URL url = new URL( strings [ 0 ]) ; try { HttpURLConnection connection = ( HttpURLConnection ) url .openConnection() ; connection .connect() ; inputStream = connection .getInputStream() ; return BitmapFactory . decodeStream ( inputStream ) ; } catch ( IOException e ) { e .printStackTrace() ; } } catch ( MalformedURLException e ) { e .printStackTrace() ; } return null; } @Override protected void onPostExecute ( Bitmap bitmap ) { ShowNotification( bitmap ) ; } }
AppBar ViewPager with  Tab layout in android  XML : <? 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" xmlns: app ="http://schemas.android.com/apk/res-auto" android :id ="@+id/mainrelative_layout_id" android :background ="@color/colorPrimary" android :theme ="@style/Appthemnoactionbar" android :layout_height ="match_parent" tools :context ="com.ruhul.scanner.Fragment_Activity.ListFragment" > <com.google.android.material.appbar.AppBarLayout android :id ="@+id/tablayoutid" android :layout_width ="match_parent" android :background ="@color/colorPrimary" android :layout_height ="?actionBarSize" > ...