在 Android 10 (Q) 中将我的应用设置为默认拨号器不询问选项?

问题描述 投票:0回答:2

我目前无法让我的应用程序向用户发送对话以在 API 29 上设置默认拨号器,但相同的代码在 Android 10 (Q) 以下运行良好。

我测试了这个 没有响应提示用户为 SMS 示例设置默认处理程序的意图也没有响应,但不适用于我的。然后我跟着https://developer.android.com/guide/topics/permissions/default-handlers但没有解决我的问题。 API 级别 29 或更高版本是否有任何特殊更改让用户设置默认应用程序?

这是我的工作代码(适用于 Android 10 以下)

TelecomManager telecomManager = (TelecomManager) getSystemService(TELECOM_SERVICE);
        if (!getPackageName().equals(telecomManager.getDefaultDialerPackage())) {
            Intent intent = new Intent(ACTION_CHANGE_DEFAULT_DIALER)
                    .putExtra(EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName());
            startActivity(intent);
        }

我在 logcat 中得到的在这里

android android-intent android-permissions telephonymanager android-10.0
2个回答
4
投票

是的,API变了,看之前API的注释在这里: https://developer.android.com/reference/android/telecom/TelecomManager#ACTION_CHANGE_DEFAULT_DIALER

在这里查看新的 API: https://developer.android.com/reference/android/app/role/RoleManager

基本上,你需要做这样的事情:

RoleManager roleManager = (RoleManager) getSystemService(Context.ROLE_SERVICE);
Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_DIALER);
startActivityForResult(Intent, CHANGE_DEFAULT_DIALER_CODE); // you need to define CHANGE_DEFAULT_DIALER as a static final int

然后您通过在您的活动中实施

onActivityResult
来捕捉响应。


0
投票

以下是我如何让我的应用符合默认电话应用的条件:

在 AndroidManifest.xml 中,我将这些添加到我的 MainActivity:

<intent-filter>
  <action android:name="android.intent.action.DIAL" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <action android:name="android.intent.action.DIAL" />
  <action android:name="android.intent.action.CALL" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="tel" />
  <data android:scheme="sip" />
</intent-filter>

并添加了这项新服务:

<service
  android:name=".InCallService"
  android:enabled="true"
  android:exported="true"
  android:permission="android.permission.BIND_INCALL_SERVICE" >
  <meta-data
    android:name="android.telecom.IN_CALL_SERVICE_UI"
    android:value="true" />
  <meta-data
    android:name="android.telecom.IN_CALL_SERVICE_CAR_MODE_UI"
    android:value="false" />
  <intent-filter>
    <action android:name="android.telecom.InCallService" />
  </intent-filter>
</service>

我实现的服务是这样的:

package <my package>

import android.app.Service
import android.content.Intent
import android.os.IBinder

// This is needed in order to allow choosing <my app> as default Phone app

class InCallService : Service() {

    override fun onBind(intent: Intent): IBinder {
        TODO("Return the communication channel to the service.")
    }
}

最后,如果需要从应用程序请求默认拨号器角色(而不仅仅是从 Android 应用程序设置,可以这样做:

val dialerRoleRequest = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) {
    Log.d(TAG, "dialerRoleRequest succeeded: ${it.resultCode == Activity.RESULT_OK}")
}
        
if (roleManager.isRoleAvailable(RoleManager.ROLE_DIALER) && 
        !roleManager.isRoleHeld(RoleManager.ROLE_DIALER))
    dialerRoleRequest.launch(roleManager.createRequestRoleIntent(RoleManager.ROLE_DIALER))

需要从 Android 应用程序设置中删除默认拨号器角色。

© www.soinside.com 2019 - 2024. All rights reserved.