Search code examples
xamarin.formsxamarin.androidgeolocationandroid-permissions

Disable Automatic Location Permission on Android Xamarin Forms


I am developing a cross platform app which uses location and of course for it to able to use the user's location it needs their permission. What I want to do is display a privacy disclosure popup and after the user reads it they can click "Access Location" and there I want the app to ask for permission to access location. However what happens is that when the app opens for the first time it automatically asks for location permission with the disclosure being underneath and I don't understand why.

This is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="7" android:versionName="7.0" package="com.mmh.app">
    <uses-sdk android:minSdkVersion="25" android:targetSdkVersion="30" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.location" android:required="true" />
    <uses-feature android:name="android.hardware.location.gps" android:required="true" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-feature android:name="android.hardware.location.network" android:required="true" />
    <application android:label="App.Android" android:usesCleartextTraffic="true" android:theme="@style/MainTheme" android:icon="@drawable/appicon">
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

And this is my MainActivity.cs

namespace App.Droid
{
    [Activity(Label = "App", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
           
   
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            
            base.OnCreate(savedInstanceState);
           // CrossCurrentActivity.Current.Activity = this;
            Rg.Plugins.Popup.Popup.Init(this);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
            //CrossCurrentActivity.Current.Init(this, savedInstanceState);

           

           CreateNotificationFromIntent(Intent);



        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        
       void CreateNotificationFromIntent(Intent intent)
        {
            if (intent?.Extras != null)
            {
                string title = intent.GetStringExtra(AndroidNotificationManager.TitleKey);
                string message = intent.GetStringExtra(AndroidNotificationManager.MessageKey);
                DependencyService.Get<INotificationManager>().ReceiveNotification(title, message);
            }
        }
        public override void OnBackPressed()
        {
            //Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed);
            return;
        }

       
    }

Any help is appreciated.

Thanks.


Solution

  • Create a new page and you make it your MainPage, just to check if the app ask at the beginning of the App or when the page is shown.