문제

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