首页 > 解决方案 > 媒体和照片的 Xamarin 和 android 权限 - 应用程序在发布模式下崩溃

问题描述

我有一个相当奇怪的问题,我认为它与权限有关。当我在调试模式下运行我的应用程序时,它运行良好,但是当我在发布模式下运行它时,它会崩溃。我认为这可能与权限有关的原因是当我在调试模式下运行应用程序时,当它部署在我的测试设备(我的三星手机)上时,它要求我允许访问照片和媒体。我已经在清单中设置了所有权限,甚至与照片和媒体远程相关,并且有代码MainActivity.cs检查这些权限,但是当我在手机上调试应用程序时,它仍然要求访问照片和媒体的权限。我在清单中没有看到任何特别提到照片的内容,并且就像我之前所说的,我在清单中授予了对甚至提到“媒体”的所有内容的访问权限。我的应用程序还可以访问互联网、前台和其他需要设置权限的区域,我已经在清单中设置了这些区域,没有问题,只有这个。我在这里想念什么?

我的MainActivity.cs代码:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        try
        {


            if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted)
            {

                ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadExternalStorage }, 1);
            }

            if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ForegroundService) != Android.Content.PM.Permission.Granted)
            {
                ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ForegroundService }, 1);
            }

            if (ActivityCompat.CheckSelfPermission(this,Android.Manifest.Permission.Internet) != Android.Content.PM.Permission.Granted)
            {
                ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.Internet }, 1);
            }

            if (ActivityCompat.CheckSelfPermission(this,Android.Manifest.Permission.AccessNetworkState) != Android.Content.PM.Permission.Granted)
            {

                ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.AccessNetworkState }, 1);
            }
            if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.AccessMediaLocation) != Android.Content.PM.Permission.Granted)
            {

                ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.AccessMediaLocation }, 1);
            }

            if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.MediaContentControl) != Android.Content.PM.Permission.Granted)
            {

                ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.MediaContentControl }, 1);
            }
            if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
            {

                ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.Camera }, 1);
            }
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.logon);
            RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;
            UserDialogs.Init(this);
            Button MyButton_Submit = FindViewById<Android.Widget.Button>(Resource.Id.button_submit);
            aTextboxUsername = FindViewById<EditText>(Resource.Id.aTextboxUsername);
            aTextboxPassword = FindViewById<EditText>(Resource.Id.aTextboxPassword);
            InputMethodManager board = (InputMethodManager)GetSystemService(Context.InputMethodService);
            board.HideSoftInputFromWindow(aTextboxUsername.WindowToken, 0);

            Button MyButton_CreateAccount = FindViewById<Android.Widget.Button>(Resource.Id.button_createaccount);
            MyButton_CreateAccount.SetBackgroundColor(Android.Graphics.Color.YellowGreen);

            MyButton_Submit.Click += async (sender, e) =>
            {
                ManualResetEvent resetEvent = new ManualResetEvent(false);

                ProgressBarHandler myprogressbar = new ProgressBarHandler(this);
                myprogressbar.show();

                var thread = new System.Threading.Thread(new ThreadStart(async delegate
                {
                    await SignOn(myprogressbar);
                    resetEvent.Set();

                }));
                thread.Start();

                await Task.Run(() => resetEvent.WaitOne());

                while (thread.ThreadState == ThreadState.Running)
                {
                    await Task.Delay(100);
                }

                myprogressbar.hide();

                if (myprogressbar.ErrorMesage != null)
                {
                    showMessage(myprogressbar.ErrorMesage);
                }
            };

            MyButton_CreateAccount.Click += (sender, e) =>
            {
                Intent intent = new Intent(this, typeof(CreateAccount));
                Bundle bundlee = new Bundle();
                intent.PutExtra("TheBundle", bundlee);
                StartActivity(intent);
            };
        }
        catch (Exception ex)
        {
            Utils.LogError("An error occured in MainActivity.cs, the error is: " + ex.Message);
        }
    }

*** 更新 ***

这是我在设备日志中看到的:

Time    Device Name Type    PID Tag Message
06-24 08:58:00.417  Samsung SM-G973U    Warning 1141    ContextImpl Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1162 com.android.server.am.AppErrors.crashApplicationInner:579 com.android.server.am.AppErrors.crashApplication:443 com.android.server.am.ActivityManagerService.handleApplicationCrashInner:12323 com.android.server.am.ActivityManagerService.handleApplicationCrash:12258 
  Force finishing activity My.App/crc64a23c41873b6fbff5.MainActivity
06-24 08:58:00.417  Samsung SM-G973U    Info    2468    dumpstate   skip_space_check : 0
06-24 08:58:00.417  Samsung SM-G973U    Info    2468    dumpstate   skeymaster_only: 0, keystore_only: 0
06-24 08:58:00.417  Samsung SM-G973U    Info    2468    dumpstate   shutdown_broadcast: 0, shutdown_delay: 0
06-24 08:58:00.417  Samsung SM-G973U    Info    2468    dumpstate   sec_mode : 1 dump_flags : 247031436, booting_delay : 0 booting_enospc : 0 sys_rescue : 0 sys_error : 0 sys_native : 0 sys_watchdog : 0 app_error : 1 app_native : 0 app_anr : 0
06-24 08:58:00.416  Samsung SM-G973U    Info    2468    dumpstate   do_zip_file: 1 do_vibrate: 0 use_socket: 0 use_control_socket: 0 do_screenshot: 0 is_remote_mode: 0 show_header_only: 0 do_start_service: 0 telephony_only: 0 wifi_only: 0 do_progress_updates: 0 fd: 10 bugreport_mode: BUGREPORT_A_ERROR dumpstate_hal_mode: DEFAULT limited_only: 0 args: 
06-24 08:58:00.416  Samsung SM-G973U    Warning 1141    ActivityManager crash : My.App,10511

标签: c#xamarinvisual-studio-2019android-permissions

解决方案


好吧,经过 2 天的痛苦过程,我(显然)想通了。

Enable Multi-Dex我在 Project->Properties->Android Options 中关闭(未选中)

该应用程序现在在发布模式下运行,没有任何问题。


推荐阅读