I'm trying to grand permissions to my android application using Plugin.Permissions and Xamarin.Forms
I'm following the provided sample, but I'm getting the following error:
04-10 17:42:09.234 I/MonoDroid(15633): UNHANDLED EXCEPTION:
04-10 17:42:09.234 I/MonoDroid(15633): System.MissingMethodException: Method not found: Android.App.Activity Xamarin.Essentials.Platform.get_CurrentActivity()
04-10 17:42:09.234 I/MonoDroid(15633): at TestApp.Views.MainPage.OnButtonClicked (System.Object sender, System.EventArgs e) [0x000c3] in C:\projects\my\android-projects\TestXamarin\TestXamarinProject\TestApp\TestApp\Views\MainPage.xaml.cs:31
04-10 17:42:09.234 I/MonoDroid(15633): at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
04-10 17:42:09.234 I/MonoDroid(15633): at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <4ccdb3137d974856b786e1aeebbfbab6>:0
04-10 17:42:09.234 I/MonoDroid(15633): at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <4ccdb3137d974856b786e1aeebbfbab6>:0
04-10 17:42:09.235 I/MonoDroid(15633): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <4ccdb3137d974856b786e1aeebbfbab6>:0
04-10 17:42:09.235 I/MonoDroid(15633): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.44(intptr,intptr)
Here is my MainActivity:
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using Plugin.Permissions;
namespace ExoPlayerVU.Droid
{
[Activity(Label = "TestApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
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);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
I'm requesting the permission on button click from MainPage.xaml.cs:
public async void OnButtonClicked(object sender, EventArgs e)
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync<StoragePermission>();
if (status != PermissionStatus.Granted)
{
// The error occurs here
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
{
await DisplayAlert("Access needed", "This application uses storage", "OK");
}
status = await CrossPermissions.Current.RequestPermissionAsync<StoragePermission>();
}
if (status == PermissionStatus.Granted)
{
// my logic
}
else
{
await DisplayAlert("Storage access denied", "Can not continue, try again.", "OK");
}
}
The Plugin.Permissions and Xamarin.Essentials version are the latest. What I'm doing wrong?
Thanks in advance
UPDATE AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.testapp" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
<application android:label="TestApp.Android"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.WRITE_PROFILE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
UPDATE 2
Also I have the application class:
[Application]
public class DemoApplication : Application
{
// ...
public PlayerApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public PlayerApplication()
{
}
public override void OnCreate()
{
base.OnCreate();
userAgent = Util.GetUserAgent(this, "TestApp");
// Tried to initialize here, but also doesn't work
//CrossCurrentActivity.Current.Init(this);
//Xamarin.Essentials.Platform.Init(this);
}
// ...
}
Xamarin Version: 16.5
Xamarin.Android SDK: 10.2
Nugets:
Xamarin.Forms: 4.4
Xamarin.Essentials: 1.5.2