Android 本地加速改为 前台服务 & 修复 PendingIntent 在 Android 12+ 上的异常 & 主活动增加参数打开对应的子页

This commit is contained in:
Aigio Liu
2022-01-26 19:24:10 +08:00
parent 25851f7070
commit adf1bf0d97
17 changed files with 713 additions and 307 deletions
@@ -0,0 +1,32 @@
using Android.App;
using Android.Content;
using Android.OS;
using System.Application.Services.Native;
using NativeService = System.Application.Services.Native.IServiceBase;
// ReSharper disable once CheckNamespace
namespace System
{
partial class ContextExtensions
{
/// <summary>
/// 启动或关闭前台服务
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="context"></param>
/// <param name="startOrStop"></param>
public static void CallForegroundService<TService>(this Context context, bool startOrStop) where TService : Service, IServiceBase
{
Intent intent = new(context, typeof(TService));
intent.SetAction(startOrStop ? NativeService.START : NativeService.STOP);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
context.StartForegroundService(intent);
}
else
{
context.StartService(intent);
}
}
}
}