簡體   English   中英

當屏幕關閉時,來自前台服務的呼叫停止工作

[英]Call from foreground service stops working when screen is off

我有以下撥打電話的代碼:

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 后,我遇到了一個新問題。

首先,我被迫添加一個新權限FOREGROUND_SERVICE 添加,前台服務按預期工作並撥打電話 - 但僅當電話處於“活動”狀態時,我的意思是當屏幕關閉時它不處於“睡眠”模式。

如果屏幕關閉 - 服務正常,我可以跟蹤活動,但無法撥打電話。

adb logcat顯示此警告(第一行是Info ,第二行是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}]

我認為您應該看看“部分喚醒鎖定”以防止手機的“睡眠模式”。

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

在您的 MainActivity.cs 中創建一個 WakeLock 對象並在您的 OnCreate 函數下配置您的喚醒鎖:

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 中添加一個權限:

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

此解決方案可以工作,但您需要小心,因為對於某些制造商而言,如果您的應用程序使用許多資源,則負責 CPU 的“始終處於活動狀態”狀態的前台服務可能會被終止。 在某些情況下,他們添加了“省電”選項模式,您需要直接在設置中禁用它才能正常運行您的應用程序。

希望這可以幫助

使用電信管理器。 是這樣的:

    Uri uri = Uri.fromParts("tel", phoneNumber, null);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            TelecomManager telecomManager = (TelecomManager) getInstance().getSystemService(Context.TELECOM_SERVICE);
            telecomManager.placeCall(uri, new Bundle());
        }
        else
        {
            Intent callIntent = new Intent(Intent.ACTION_CALL, uri);
            callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(callIntent);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM