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 ) ; } }
  Kotlin Function function means use big problems divided by submodule and easily problems solved.   It makes reusability of code and makes the program more manageable There are two types of functions: Standard library function User-defined function Standard Library Function Kotlin Standard library function is built-in library functions import java.time.LocalDateTime fun main (args: Array < String >) { val current = LocalDateTime.now() println( "Current Date and Time is: $current " ) } User-defined Function A user-defined is created by the user. User-defined function takes the parameter(s), perform an action and return the result of that action as a value. fun  main(args: Array<String>){       sum()       print( "code after sum" )   }   fun  sum(){       var num1 = 5        var...