How to detect if device is capable of calling and

2019-01-27 12:46发布

问题:

Some devices ie. Galaxy Tablet 10.1 can only send SMS, but cannot call. Some other devices like Asus Transformer don't even have SIM card.

How can I detect if device can makes calls? And how can I detect if device can send SMS?

回答1:

Maybe you can query the PackageManager whether the system contains any component that can respond to ACTION_CALL and ACTION_SENDTO intents? You might need to add the "tel:" and "smsto:" scheme in the URI.



回答2:

Using this technic you can test all sorts of things too e.g. compass, is location available

    PackageManager pm = getBaseContext().getPackageManager();
    pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);


回答3:

That should do it:

 PackageManager pm = this.getPackageManager();

 if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
     System.out.println("horray");
 } else {
     System.out.println("nope");
 }


回答4:

You can use the below method to check if sms feature is supported or not:

private void sendSms(String theNumber, String theMsg) {
        // TODO Auto-generated method stub
        String SENT = "Message Sent";
        String DELIVERED = "Message Delivered";

        PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(DELIVERED), 0);

        registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                switch(getResultCode()){
                case Activity.RESULT_OK:   Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_SHORT).show();
                                            break;
                case Activity.RESULT_CANCELED: Toast.makeText(getApplicationContext(), "Message Not Sent", Toast.LENGTH_SHORT).show();
                                                break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:  Toast.makeText(getApplicationContext(), "No Service Available", Toast.LENGTH_SHORT).show();
                                                           break;
                }
            }
        }, new IntentFilter(SENT));

        registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                switch(getResultCode()){
                case Activity.RESULT_OK:   Toast.makeText(getApplicationContext(), "Message Delivered", Toast.LENGTH_SHORT).show();
                                            break;
                case Activity.RESULT_CANCELED: Toast.makeText(getApplicationContext(), "Message Not Delivered", Toast.LENGTH_SHORT).show();
                                                break;

                }
            }
        }, new IntentFilter(DELIVERED));


        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(theNumber, null, theMsg, sentPI, deliveredPI);
    }


回答5:

You can just wrap your code in try/catch. It works in all cases, even with the last api changes about sms sending.

try{
    // code that use telephony features
}
catch(Exception e){
    // code that doesn't use telephony features
}


回答6:

Here is what I make to check is SMS available.

public boolean isAvailable(Context context, Intent intent) {
    final PackageManager mgr = context.getPackageManager();
    List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

which is taken from developer.android.com.

And create an Intent to check like this:

Uri smsToUri = Uri.parse("smsto:");
Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
if (isAvailable(intent)) {
    // do whatever you like.
}