创建通知时,Android有时会引发致命错误

时间:2019-06-02 03:56:05

标签: c# xamarin.forms xamarin.android

我对xamarin非常陌生,并创建了下载通知。它可以在大多数时间工作,但有时在创建通知后,该应用会出现致命错误并关闭。

我只有这种融洽的关系,我不知道该如何追踪。

=================================================================
    Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================
06-02 05:39:28.296 E/mono-rt (16761): /proc/self/maps:
06-02 05:39:28.296 E/mono-rt (16761): 12c00000-16f40000 rw-p 00000000 00:01 25459                              /dev/ashmem/dalvik-main space (region space)_4473_4473 (deleted)
06-02 05:39:28.296 E/mono-rt (16761): 16f40000-172c0000 rw-p 04340000 00:01 25459                              /dev/ashmem/dalvik-main space (region space)_4473_4473 (deleted)
06-02 05:39:28.296 E/mono-rt (16761): 172c0000-17340000 ---p 046c0000 00:01 25459                              /dev/ashmem/dalvik-main space (region space)_4473_4473 (deleted)
06-02 05:39:28.296 E/mono-rt (16761): 17340000-173c0000 rw-p 04740000 00:01 25459                              /dev/ashmem/dalvik-main space (region space)_4473_4473 (deleted)
06-02 05:39:28.296 E/mono-rt (16761): 173c0000-18380000 ---p 047c0000 00:01 25459                              /dev/ashmem/dalvik-main space (region space)_4473_4473 (deleted)
06-02 05:39:28.296 E/mono-rt (16761): 18380000-183c0000 rw-p 05780000 00:01 25459                              /dev/ashmem/dalvik-main space (region space)_4473_4473 (deleted)
06-02 05:39:28.297 E/mono-rt (16761): 183c0000-18400000 ---p 057c0000 00:01 25459                              /dev/ashmem/dalvik-main space (region space)_4473_4473 (deleted)

=================================================================
    Basic Fault Adddress Reporting
=================================================================
Memory around native instruction pointer (0x6f51ecb678):0x6f51ecb668  00 1c 40 b9 c0 03 5f d6 fd 7b bf a9 fd 03 00 91  ..@..._..{......
0x6f51ecb678  08 20 40 b9 a8 00 c8 37 88 01 e0 37 00 00 40 f9  . @....7...7..@.
0x6f51ecb688  fd 7b c1 a8 c0 03 5f d6 60 08 00 b0 01 0a 00 f0  .{...._.`.......
0x6f51ecb698  03 0a 00 f0 00 24 06 91 21 80 08 91 63 8c 0a 91  .....$..!...c...

No native Android stacktrace (see debuggerd output).

这是我启动服务的方式

  Methods.OnVideoDownload += new Action<Manager.Models.Container.YoutubeFileDownloadItem>((video) =>
    {
        if (!ObjectCacher.DownloadingFiles.ContainsKey(video.VideoId))
        {
            ObjectCacher.DownloadingFiles.Add(video.VideoId, video);
            var intent = new Intent(this, typeof(FileService));
            intent.PutExtra(FileService.URL, new AppManager().GetVideoStreamUrl(video.VideoId, 18));
            intent.PutExtra(FileService.Title, video.Title);
            intent.PutExtra(FileService.DirectoryName, Path.Combine(Methods.LocalMusicFolder, video.Playlist));
            intent.PutExtra(FileService.FileName, video.GenerateLocalPath());

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                var servive = this.StartForegroundService(intent);
            }
            else
            {
                this.StartService(intent);
            }
        }
    });

这是onstartCommand,它在返回StartCommandResult.Sticky;返回后将关闭应用程序

 public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        PendingIntent pendingIntent = PendingIntent.GetActivity(MainActivity.Current, 0, intent, 0);
        var url = intent.GetStringExtra(URL);
        var fileName = intent.GetStringExtra(FileName);
        var directory = intent.GetStringExtra(DirectoryName);
        var title = intent.GetStringExtra(Title);

        intent.AddFlags(ActivityFlags.ClearTop);
        var nb = NotificationHelper.GetNotification1(GetString(Resource.String.Downloading) + " " + title, GetString(Resource.String.DownloadProgress), pendingIntent);


        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            StartForeground(NotificationId, nb.Build());
        }


        var _downlodFile = new DownlodFile(url, directory, fileName, new Action<int>((procent) =>
        {
            nb.SetContentText("")
            .SetContentTitle(GetString(Resource.String.Downloading) + $" ({procent}%) " + title)
            .SetProgress(100, procent, false);
            NotificationHelper.Notify(NotificationId, nb);
        }), new Action(async () =>
        {
            try
            {
                await Methods.DownloadCompleted(fileName);
            }
            catch (Exception ex)
            {
            }
            nb.SetOnlyAlertOnce(false).SetProgress(0, 0, false)
            .SetContentText(GetString(Resource.String.Finished));
            await Task.Delay(100);
            NotificationHelper.Notify(NotificationId, nb);
            ObjectCacher.RandomIdCacher.Remove(NotificationId);
            nb.Dispose();
            NotificationHelper.Dispose();
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                StopForeground(true);
            }

        }), new Action<Exception>((e) =>
        {
            nb.SetProgress(0, 0, false).SetContentText("Error: Something went wrong.\n Please try agen later.");
            NotificationHelper.Notify(NotificationId, nb);
            ObjectCacher.RandomIdCacher.Remove(NotificationId);
            nb.Dispose();
            NotificationHelper.Dispose();
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                StopForeground(true);
            }
        }));
        _downlodFile.Download();
        NotificationHelper.Notify(NotificationId, nb);
        return StartCommandResult.Sticky;
    }
}

导致此问题的原因甚至是如何追查此问题的任何想法。

1 个答案:

答案 0 :(得分:1)

可能有多种原因,这取决于您正在运行的设备(电话/仿真器,Android版本,是在授予用户权限之前/之后的错误,但权限被拒绝)。

我怀疑的原因是您没有正确处理权限,另一个可能的原因可能是您没有在UI线程中运行文件下载工作。因此,将行更新为类似

this.Activity.RunOnUiThread(() =>
{
    _downlodFile.Download();
});

您可以

  1. 创建系统捕获点异常,
  2. 启动分析器以跟踪错误
  3. (并且应该)使用try-catch语句, 基本上可以弄清楚代码崩溃的行,以及内部堆栈跟踪是什么。
相关问题