且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:如何制作默认的拨号器应用程序?

更新时间:2022-12-21 16:20:41

要设置默认的拨号器应用程序,您需要做两件事:

1.在您的android清单中添加以下权限

to make default dialer app, you need to do 2 things :

1. add the following permissions in your android manifest

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

  1. 实际执行检查:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_layout)
    ...
    checkDefaultDialer()
    ...
}
const val REQUEST_CODE_SET_DEFAULT_DIALER=200

private fun checkDefaultDialer() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        return

    val telecomManager = getSystemService(TELECOM_SERVICE) as TelecomManager
    val isAlreadyDefaultDialer = packageName == telecomManager.defaultDialerPackage
    if (isAlreadyDefaultDialer)
        return
    val intent = Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)
                .putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, packageName)
    startActivityForResult(intent, REQUEST_CODE_SET_DEFAULT_DIALER)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when (requestCode) {
        REQUEST_CODE_SET_DEFAULT_DIALER -> checkSetDefaultDialerResult(resultCode)
    }
}

private fun checkSetDefaultDialerResult(resultCode: Int) {
    val message = when (resultCode) {
        RESULT_OK       -> "User accepted request to become default dialer"
        RESULT_CANCELED -> "User declined request to become default dialer"
        else            -> "Unexpected result code $resultCode"
    }

    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

}