如何检测设备是否能够进行呼叫和消息传递

时间:2011-09-15 11:00:01

标签: android telephony

有些设备即。 Galaxy Tablet 10.1只能发短信,但无法拨打电话。像华硕Transformer这样的其他设备甚至没有SIM卡。

如何检测设备是否可以拨打电话?如何检测设备是否可以发送短信?

6 个答案:

答案 0 :(得分:27)

使用这种技术你也可以测试各种各样的东西,例如罗盘,是可用的位置

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

答案 1 :(得分:6)

也许你可以query the PackageManager系统是否包含任何可以响应ACTION_CALL和ACTION_SENDTO意图的组件?您可能需要在URI中添加“tel:”和“smsto:”方案。

答案 2 :(得分:4)

应该这样做:

 PackageManager pm = this.getPackageManager();

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

答案 3 :(得分:1)

您可以使用以下方法检查是否支持短信功能:

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);
    }

答案 4 :(得分:0)

您可以将代码包装在try / catch中。它适用于所有情况,即使是关于短信发送的最后一次api更改。

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

答案 5 :(得分:0)

我要检查的是SMS可用。

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

取自developer.android.com

创建一个Intent来检查:

fbShare.completionHandler = { (result:SLComposeViewControllerResult) -> Void in
            switch result {
            case SLComposeViewControllerResult.cancelled:
                print("Cancelled") 
                break

            case SLComposeViewControllerResult.done:
                print("Done")
                break
            }
        }