本文介绍了当屏幕关闭时,来自前台服务的呼叫停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下拨打电话的代码:

I have the following code that makes a phone call:

public static void CallPhoneNumber(this Context context, string phoneNumber)
{
    var uri = Android.Net.Uri.Parse("tel:" + phoneNumber);
    var callIntent = new Intent(Intent.ActionCall, uri);
    callIntent.AddFlags(ActivityFlags.NewTask);
    callIntent.AddFlags(ActivityFlags.FromBackground);
    context.StartActivity(callIntent);
}

我在正在运行的前台服务中拨打电话.基本上,该服务会检测条件(在我的情况下为 GPS 位置)并拨打电话.它在我的 Pixel 2XL 和 Android 9 上运行良好.但在升级到 Android 10 后,我遇到了一个新问题.

I make a phone call inside a running foreground service. Basically the service detects conditions (in my case GPS location) and makes a phone call. It worked just fine with my Pixel 2XL and Android 9. But after upgrade to Android 10 I faced to a new problem.

首先,我被迫添加了一个新的权限 FOREGROUND_SERVICE.添加,前台服务按预期工作并拨打电话 - 但仅当电话活动"时,我的意思是它不在睡眠"状态.屏幕关闭时的模式.

First of all, I was forced to add a new permission FOREGROUND_SERVICE. Added, the foreground service works as expected and makes phone calls - but only when phone is "active", I mean it is not in a "sleep" mode when the screen is turned off.

如果屏幕关闭 - 服务有效,我可以跟踪活动,但它不会拨打电话.

If the screen is off - the service works, I can track the activity, but it doesn't make a phone call.

adb logcat 显示此警告(第一行是 Info,第二行是 Warning):

The adb logcat shows this warning (first line is Info, the second is Warning):

02-04 20:48:00.923  1315  7951 I ActivityTaskManager: START u0 {act=android.intent.action.CALL dat=tel:xxxxxxxxxxxx flg=0x10000004 cmp=com.android.server.telecom/.components.UserCallActivity} from uid 10174
02-04 20:48:00.924  1315  7951 W ActivityTaskManager: Background activity start [callingPackage: MyApp; callingUid: 10175; isCallingUidForeground: false; isCallingUidPersistentSystemProcess: false; realCallingUid: 10174; isRealCallingUidForeground: false; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; isBgStartWhitelisted: false; intent: Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxxxx flg=0x10000004 cmp=com.android.server.telecom/.components.UserCallActivity }; callerApp: ProcessRecord{43f3a72 13957:MyApp/u0a174}]

推荐答案

我觉得你应该看看部分唤醒锁"以防止睡眠模式"电话.

I think you should take a look at "partial wakelock" to prevent the "sleep mode" of the phone.

https://developer.android.com/training/scheduling/wakelock

在您的 MainActivity.cs 中创建一个 WakeLock 对象并在您的 OnCreate 函数下配置您的唤醒锁:

in your MainActivity.cs create a WakeLock object and under your OnCreate function configure your wakelock :

private PowerManager.WakeLock wl = null;

protected override void OnCreate(Bundle savedInstanceState)
{
    // ... Your own code here
    PowerManager pmanager = (PowerManager)this.GetSystemService("power");
    wl = pmanager.NewWakeLock(WakeLockFlags.Partial, "myapp_wakelock");
    wl.SetReferenceCounted(false);
    wl.Acquire();
 }

不要忘记在 AndroidManifest.xml 中添加权限:

Don't forget to add a permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.WAKE_LOCK" />

此解决方案可以工作,但您需要小心,因为对于某些制造商而言,如果您的应用程序使用许多资源,则负责状态always alive"的前台服务将无法正常工作.的 CPU 可以被杀死.在某些情况下,他们会添加一个电池保护程序".选项模式,您需要直接在设置中禁用它才能毫无问题地运行您的应用.

This solution can work but you're need to be carreful because for some manufacturer, if your app use to many ressources the foreground service responsible of the state "always alive" of the CPU can be killed. In some cases, they add a "battery saver" option mode and you need to disable it directly in settings to run your app without issues.

希望能帮到你

这篇关于当屏幕关闭时,来自前台服务的呼叫停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:12
查看更多