使用Android 10上的action_call意图进行呼叫不起作用。

huangapple go评论65阅读模式
英文:

Make a call using action_call intent on Android 10 doesn't work

问题

This works on the previous version of Android but on Android 10 it no longer works. any ideas how to solve this problem. any help would be greatly appreciated. I have tried with intent action_call and placeCall from telecomManager.

/**
 * Call a given number
 *
 * @param context
 * @param number
 */
public static void call(@NotNull Context context, @NotNull String number) {
    try {
        // Create call intent
        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(number)));
        // Handle sim card selection
        //            int simCard = getSimSelection(context);
        //            Timber.d("simcard "+simCard);
        //            if (simCard != -1) callIntent.putExtra("com.android.phone.extra.slot", simCard);
        
        callIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
        // Start the call
        context.startActivity(callIntent);
    } catch (SecurityException e) {
        Toast.makeText(context, "Couldn't make a call due to security reasons", Toast.LENGTH_LONG).show();
    } catch (NullPointerException e) {
        Toast.makeText(context, "Couldn't make a call, no phone number", Toast.LENGTH_LONG).show();
    }
}

/**
 * Places a new outgoing call to the provided address using the system telecom service with
 * the specified intent.
 *
 * @param activity       {@link Activity} used to start another activity for the given intent
 * @param telecomManager the {@link TelecomManager} used to place a call, if possible
 * @param intent         the intent for the call
 */
public static boolean placeCall(@Nullable FragmentActivity activity,
                                @Nullable TelecomManager telecomManager, @Nullable Intent intent) {
    if (activity == null || telecomManager == null || intent == null) {
        return false;
    }
    if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        return false;
    }
    telecomManager.placeCall(intent.getData(), intent.getExtras());
    return true;
    //        activity.startActivityForResult(intent, 1291);
    //        return true;
}

这是您提供的代码的翻译部分。

英文:

This works on the previous version of android but on android 10 it no longer works . any ideas how to solve this problem. any help would be greatly appreciated . I have tried with intent action_call and placeCall from telecomManager.

        /**
* Call a given number
*
* @param context
* @param number
*/
public static void call(@NotNull Context context, @NotNull String number) {
try {
// Create call intent
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(number)));
// Handle sim card selection
//            int simCard = getSimSelection(context);
//            Timber.d("simcard "+simCard);
//            if (simCard != -1) callIntent.putExtra("com.android.phone.extra.slot", simCard);
callIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
// Start the call
context.startActivity(callIntent);
} catch (SecurityException e) {
Toast.makeText(context, "Couldn't make a call due to security reasons", Toast.LENGTH_LONG).show();
} catch (NullPointerException e) {
Toast.makeText(context, "Couldnt make a call, no phone number", Toast.LENGTH_LONG).show();
}
}
/**
* Places a new outgoing call to the provided address using the system telecom service with
* the specified intent.
*
* @param activity       {@link Activity} used to start another activity for the given intent
* @param telecomManager the {@link TelecomManager} used to place a call, if possible
* @param intent         the intent for the call
*/
public static boolean placeCall(@Nullable FragmentActivity activity,
@Nullable TelecomManager telecomManager, @Nullable Intent intent) {
if (activity == null || telecomManager == null || intent == null) {
return false;
}
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return false;
}
telecomManager.placeCall(intent.getData(), intent.getExtras());
return true;
//        activity.startActivityForResult(intent, 1291);
//        return true;
}

答案1

得分: 2

尝试添加callIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);

英文:

try with adding callIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);

答案2

得分: 1

你是否尝试从后台服务启动通话?

Android 10 限制了从后台启动活动。有一些例外情况。在我看来,请求 "SYSTEM_ALERT_WINDOW" 权限是最简单的方法。

https://developer.android.com/guide/components/activities/background-starts

https://stackoverflow.com/a/59421118/11982611

private void RequestPermission() {
    // 检查是否是 Android M 或更高版本
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // 显示警告对话框,告知用户需要单独的权限
        // 如果用户喜欢,可以启动设置活动
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getActivity().getPackageName()));
        startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(getContext())) {
                PermissionDenied();
            } else {
                // 权限已授予-系统将正常工作
            }
        }
    }
}
英文:

Are you trying to start a call from a background service?

Android 10 restricted to starting activity from background. There are some exclusions for this. In my view asking for "SYSTEM_ALERT_WINDOW" permission is the easiest one.

https://developer.android.com/guide/components/activities/background-starts

https://stackoverflow.com/a/59421118/11982611

private void RequestPermission() {
// Check if Android M or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Show alert dialog to the user saying a separate permission is needed
// Launch the settings activity if the user prefers
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getActivity().getPackageName()));
startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(getContext())) {
PermissionDenied();
}
else
{
//Permission Granted-System will work
}
}
}

答案3

得分: 0

如果在设置中拨打电话之前未选择帐户,则需要将帐户句柄作为额外信息发送。

try {
    List<PhoneAccountHandle> phoneAccountHandleList = TelecomUtil.getTelecomManager(context).getCallCapablePhoneAccounts();
    int simCard = getSimSelection(context);

    // 创建拨打电话的意图
    Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(number)));

    if (phoneAccountHandleList != null && !phoneAccountHandleList.isEmpty()) {
        callIntent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandleList.get(simCard));
    }
    // 处理SIM卡选择
    Timber.d("simcard %s", simCard);
    if (simCard != -1) callIntent.putExtra("com.android.phone.extra.slot", simCard);
    // 启动电话拨打
    context.startActivity(callIntent);

} catch (SecurityException e) {
    Toast.makeText(context, "由于安全原因,无法拨打电话", Toast.LENGTH_LONG).show();
} catch (NullPointerException e) {
    Toast.makeText(context, "无法拨打电话,没有电话号码", Toast.LENGTH_LONG).show();
}
英文:

If the account is not selected before placing a call in setting it requires you to send a account handle as an extra.

  try {
List&lt;PhoneAccountHandle&gt; phoneAccountHandleList = TelecomUtil.getTelecomManager(context).getCallCapablePhoneAccounts();
int simCard = getSimSelection(context);
// Create call intent
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(&quot;tel:&quot; + Uri.encode(number)));
if (phoneAccountHandleList != null &amp;&amp; !phoneAccountHandleList.isEmpty()) {
callIntent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandleList.get(simCard));
}
//            // Handle sim card selection
Timber.d(&quot;simcard %s&quot;, simCard);
if (simCard != -1) callIntent.putExtra(&quot;com.android.phone.extra.slot&quot;, simCard);
//            // Start the call
context.startActivity(callIntent);
} catch (SecurityException e) {
Toast.makeText(context, &quot;Couldn&#39;t make a call due to security reasons&quot;, Toast.LENGTH_LONG).show();
} catch (NullPointerException e) {
Toast.makeText(context, &quot;Couldnt make a call, no phone number&quot;, Toast.LENGTH_LONG).show();
}

huangapple
  • 本文由 发表于 2020年8月13日 19:30:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63394205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定