I have a feature that uses the tel:// URI in an intent, this would launch phone apps and also give the choice of using the built in phone, so for devices without cellular signal I put in a condition to check

if(pm.hasSystemFeature("android.hardware.telephony")) where pm is PackageManager object.

Some devices will crash if this condition is not there, despite being able to install the app based on manifest permissions.

But there are also VOIP apps which will take the tel:// intent but don't actually use the telephony hardware.

is there a way I can check to see if the user's device has something that can use the tel URI instead of checking what hardware features they have?

有帮助吗?

解决方案

You can check if the system has any activities that can receive the intent like so:

private void tryOpenDialer(String phoneNumber) {
    if (TextUtils.isEmpty(phoneNumber)) {
        // invalid input - return or throw IllegalArgumentException
        return;
    }
    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
    PackageManager pkgManager = getPackageManager();
    List<ResolveInfo> activities = pkgManager.queryIntentActivities(intent, 0);
    if (activities.size() > 0) {
        context.startActivity(intent)
    } else {
        // if you want, pop up a toast or dialog telling the user there is
        // nothing on the device to handle this action
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top