Supporting android devices with phone and without

2020-06-26 13:21发布

问题:

I have an application that will run on devices that have phone capabilities and that don't. Here are some of my queries:

1) Will i be able to support both type of devices. 2) For the devices that have phone capabilities, i need to enable calling feature. and the device that do not have phone capabilities i will disable calling functionality.

I am not very clear about the <user-permissions> and <user-features> concept, is there a way to specify phone <user-features>

回答1:

If you're not distributing your app through the Market and if you don't care about following recommended practices, then you should only need <uses-permission> tags for any permissions the app uses. However, to allow the correct devices to access the app through the Market, you will need both <uses-permission> and <uses-feature> tags.

<uses-permission> is a request to give your application the authority to take a certain kind of action. When preparing to install your app, the user can review the requested permissions and decide whether to to continue with the installation. If the app, for example, attempts to make a phone call without declaring the "android.permission.CALL_PHONE" permission, then the attempt will fail. See here for a list of base platform permissions.

<uses-permission> is also used by the Market for implicit feature requirements. If your app uses a permission that needs telephony hardware, then the Market will assume telephony hardware is required, and the app will not be available to a device that lacks telephony hardware.

<uses-feature> can be used to inform the Market either that a certain feature is required or that the feature is desirable but not required. The tag will override any features implied by <uses-permission>. If, for example, you specify <uses-feature android:name="android.hardware.telephony" android:required="false" />, then telephony is not required, regardless of what permissions are requested.

To see how <uses-permission> and <uses-feature> interact to create the Market filters, see here.

To check at run time whether a feature is available, it looks like you can use PackageManager.hasSystemFeature():

Context context;    // Some object, such as Activity, that extends Context
// ...
boolean hasTelephony = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
if (hasTelephony) {
    // ...
}
else {
    // ...
}


回答2:

Well, i dont know about a really simple way to do it, but you might want to use the method below to get the phone number of the phone and if it returns null, it probably means that phone functions are not available and it is a tablet or something like that (android powered microwave oven? :p )

private String getMyPhoneNumber() {
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    return mTelephonyMgr.getLine1Number();
}

Also , be sure to add the permission "android.permission.READ_PHONE_STATE" on your android manifest file. Good luck :)