如何检测设备是否能够通话和发短信

发布于 2024-12-04 11:35:21 字数 124 浏览 0 评论 0原文

一些设备即。 Galaxy Tablet 10.1 只能发送短信,但不能打电话。其他一些设备(例如 Asus Transformer)甚至没有 SIM 卡。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

望笑 2024-12-11 11:35:21

使用这种技术,您也可以测试各种东西,例如指南针,位置是否可用

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

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);
去了角落 2024-12-11 11:35:21

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

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.

坐在坟头思考人生 2024-12-11 11:35:21

应该这样做:

 PackageManager pm = this.getPackageManager();

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

That should do it:

 PackageManager pm = this.getPackageManager();

 if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
     System.out.println("horray");
 } else {
     System.out.println("nope");
 }
七七 2024-12-11 11:35:21

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

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

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);
    }
三岁铭 2024-12-11 11:35:21

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

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

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
}
给不了的爱 2024-12-11 11:35:21

这是我检查短信是否可用的方法。

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

取自 developer.android.com

并创建一个 Intent 来检查,如下所示:

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

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文