我正在使用
CallScreeningService
API 构建 Android 呼叫筛选应用程序。当我运行应用程序时,它注册服务正常,但是当我调用模拟器时,出现以下异常:
Java.Lang.RuntimeException:'无法创建服务com.katoapps.callblocker.CallBlockerService:java.lang.ClassNotFoundException:在路径上找不到类“com.katoapps.callblocker.CallBlockerService”:DexPathList [[zip文件"/data/app/~~OvboC6Rbk2k7eFeBnJFlKg==/com.katoapps.callblocker-aJoY2QavLWrfwVyoFIXSMA==/base.apk"],nativeLibraryDirectories=[/data/app/~~OvboC6Rbk2k7eFeBnJFlKg==/com.katoapps.callblocker-aJoY2QavLWrfwV yoFIXSMA= =/lib/x86_64, /data/app/~~OvboC6Rbk2k7eFeBnJFlKg==/com.katoapps.callblocker-aJoY2QavLWrfwVyoFIXSMA==/base.apk!/lib/x86_64, /system/lib64, /system_ext/lib64]]'
这里是
CallBlockerService
(位于/Platforms/Android/CallBlockerService.cs)
namespace CallBlocker;
using Android.App;
using Android.Telecom;
[Service(Exported = true, Permission = "android.permission.BIND_SCREENING_SERVICE")]
[IntentFilter(new string[] { "android.telecom.CallScreeningService" })]
public class CallBlockerService : CallScreeningService
{
public override void OnScreenCall(Call.Details callDetails)
{
}
}
注册服务的主要活动:
using Android.App;
using Android.App.Roles;
using Android.Content;
using Android.Content.PM;
using Android.OS;
namespace CallBlocker;
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
private static int REQUEST_ID = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RoleManager roleManager = (RoleManager)GetSystemService(RoleService);
Intent intent = roleManager.CreateRequestRoleIntent(RoleManager.RoleCallScreening);
StartActivityForResult(intent, REQUEST_ID);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == REQUEST_ID)
{
if (resultCode == Result.Ok)
{
// Your app is now the call screening app
}
else
{
// Your app is not the call screening app
}
}
}
}
Android 清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.katoapps.callblocker">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:label="Call Blocker">
<service android:name=".CallBlockerService"
android:exported="true"
android:permission="android.permission.BIND_SCREENING_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallScreeningService" />
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="29" />
</manifest>
以及项目信息(不确定是否相关)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0-android;</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>CallBlocker</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Display name -->
<ApplicationTitle>CallBlocker</ApplicationTitle>
<!-- App Identifier -->
<ApplicationId>com.katoapps.callblocker</ApplicationId>
<ApplicationIdGuid>025a85bb-9e56-4624-a3cf-3797a5a01bd2</ApplicationIdGuid>
<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">29.0</SupportedOSPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-android|AnyCPU'">
<ApplicationId>com.katoapps.callblocker</ApplicationId>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-android|AnyCPU'">
<ApplicationId>com.katoapps.callblocker</ApplicationId>
</PropertyGroup>
<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
</ItemGroup>
</Project>
为什么应用程序找不到我的
CallBlockerService
?
反编译 APK 会显示此 Android 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" android:compileSdkVersion="33" android:compileSdkVersionCodename="13" package="com.katoapps.callblocker" platformBuildVersionCode="33" platformBuildVersionName="13">
<application android:label="Call Blocker" android:icon="@mipmap/appicon" android:name="crc6474c5bb46df487c36.MainApplication" android:debuggable="true" android:allowBackup="true" android:supportsRtl="true" android:extractNativeLibs="true" android:roundIcon="@mipmap/appicon_round" android:appComponentFactory="androidx.core.app.CoreComponentFactory">
<service android:name="com.katoapps.callblocker.CallBlockerService" android:permission="android.permission.BIND_SCREENING_SERVICE" android:exported="true">
<intent-filter>
<action android:name="android.telecom.CallScreeningService"/>
</intent-filter>
</service>
<service android:name="crc6474c5bb46df487c36.CallBlockerService" android:permission="android.permission.BIND_SCREENING_SERVICE" android:exported="true">
<intent-filter>
<action android:name="android.telecom.CallScreeningService"/>
</intent-filter>
</service>
...
</application>
</manifest>
编译器修改 Android 清单并自动在 GUID 名称下添加服务,因此解决方法是简单地从清单中删除我的服务。