How to detect if device is capable of calling and messaging

后端 未结 6 1367
滥情空心
滥情空心 2020-12-20 12:03

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 devic

相关标签:
6条回答
  • 2020-12-20 12:39

    That should do it:

     PackageManager pm = this.getPackageManager();
    
     if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
         System.out.println("horray");
     } else {
         System.out.println("nope");
     }
    
    0 讨论(0)
  • 2020-12-20 12:40

    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);
    
    0 讨论(0)
  • 2020-12-20 12:40

    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.
    }
    
    0 讨论(0)
  • 2020-12-20 12:55

    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);
        }
    
    0 讨论(0)
  • 2020-12-20 12:55

    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
    }
    
    0 讨论(0)
  • 2020-12-20 12:59

    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.

    0 讨论(0)
提交回复
热议问题