xamarin.forms - 当前台服务启动 xamarin.form android 时,应用程序在 android 8.0 上崩溃
问题描述
当我的应用程序进入后台时,我启动前台服务。那时我进入设置页面并将权限状态更改为拒绝相机。我的应用在 android 8.0 上崩溃了。
我的前台服务代码如下:
namespace MyProj.Droid.Services
{
[Service]
public class MyProjService : Service
{
CancellationTokenSource _cts;
private static ILogger logger = DependencyService.Get<ILogManager>().GetLog();
public const string LOCATION_CHANNEL = "default";
NotificationManager manager;
NotificationCompat.Builder notification;
public override void OnCreate()
{
base.OnCreate();
manager = (NotificationManager)Forms.Context.GetSystemService("notification");
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
StartLocationServiceForeground();
return StartCommandResult.Sticky;
}
void StartLocationServiceForeground()
{
try
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var chan1 = new NotificationChannel(LOCATION_CHANNEL,
new Java.Lang.String("Primary"), NotificationImportance.High);
manager.CreateNotificationChannel(chan1);
notification = new NotificationCompat.Builder(Forms.Context, LOCATION_CHANNEL);
notification.SetOngoing(true)
.SetSmallIcon(Resource.Drawable.icon_transparent)
.SetContentTitle("MyProj 24x7 Trucker is running background")
.SetContentText("Tab for more information or to stop the app")
.SetColor(0x9c6114)
.SetPriority(NotificationCompat.PriorityHigh);
StartForeground(1, notification.Build());
}
}
catch(System.Exception ex)
{
}
}
public override void OnDestroy()
{
StopForeground(true);
if (manager!=null)
{
manager.CancelAll();
}
base.OnDestroy();
}
}
}
任何人都可以请帮助解决这个问题。
解决方案
推荐阅读
- delphi - Delphi Indy idHTTP 导致 Mac 应用程序退出
- r - 如何将平均值添加到分组的 bwplot Lattice R
- node.js - Websocket 连接失败,意外响应代码:404
- c++ - 解析 text/*.h 文件以识别和提取函数头
- javascript - 未捕获的 ReferenceError:使用 Sync/Await 时的 regeneratorRuntime
- radio-button - 代号中的 RadioButton 更改复选标记或颜色样式
- java - 使用 jdeps + jlink + jpackage 构建 windows 自包含应用程序后的 EXCEPTION_ACCESS_VIOLATION
- java - 为什么用一些字符串文字与其他最终的字符串变量连接来初始化字符串变量?
- python - 在字典中增加一个值
- python - 为什么python中的math.pi函数调用后没有括号?