读取设备的电话号码将引发NullPointerException异常 [英] Reading device phone number throws NULLPointerException

查看:213
本文介绍了读取设备的电话号码将引发NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读通过以下code设备的电话号码。当电话号码不可用我读了subcriber ID。它的工作原理在某些手机和某些设备抛出空指针异常。该设备日志显示,我在下面的行获得空指针异常

 如果(MyPhoneNumber.equals())
 

请让我知道如何使它在所有设备正常工作。

  TelephonyManager TMGR =(TelephonyManager)ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE);

串MyPhoneNumber = tMgr.getLine1Number();


如果(MyPhoneNumber.equals())
             MyPhoneNumber = tMgr.getSubscriberId();
 

解决方案

电话号码不可用的SIM卡,每个运营商,像印度辛不要有电话号码的任何内存,因此我们不能得到的电话号码这些连接。然而,一些国家和运营商都存储在SIM卡的电话号码,我们可以得到这些。为了使这项工作的所有设备,我们可以采用两种策略:

  1. 要避免空指针异常,我们可以捕捉到错误,并相应工作。像:

      TelephonyManager TMGR =(TelephonyManager)
                     ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE);
    
    字符串MyPhoneNumber =0000000000;
    
    尝试
    {
        MyPhoneNumber = tMgr.getLine1Number();
    }
    赶上(NullPointerException异常前)
    {
    }
    
    如果(MyPhoneNumber.equals())
        MyPhoneNumber = tMgr.getSubscriberId();
     

  2. 或者我们可以有一个短信网关,每当我们需要的电话号码,我们可以发送短信到网关,再部署一个web服务返回的号码,短信网关收到消息,然而这解决方案是昂贵的。

I am trying to read the phone number of the device using the following code. When phone number is not available I read the subcriber id. It works in some phones and throws NULL pointer exception in some devices. The device log shows I am getting NULL pointer exception in the following line

if(MyPhoneNumber.equals(""))

Please let me know how to make it work in all devices.

TelephonyManager tMgr =(TelephonyManager)ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE); 

String MyPhoneNumber = tMgr.getLine1Number(); 


if(MyPhoneNumber.equals(""))
             MyPhoneNumber = tMgr.getSubscriberId();

解决方案

Phone numbers are not available on SIM for each operators, like in india Sim dont have phone numbers in any memory, So WE cant get phone number from these connection. However, some countries, and operators have stored phone numbers on SIM, and we can get those. TO make this to work for all devices we can employ two strategies:

  1. TO avoid null pointer exception, we can catch the error and work accordingly. Like:

    TelephonyManager tMgr = (TelephonyManager) 
                     ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE);
    
    String MyPhoneNumber = "0000000000";
    
    try 
    {
        MyPhoneNumber =tMgr.getLine1Number();
    }
    catch(NullPointerException ex)
    {
    }
    
    if(MyPhoneNumber.equals("")) 
        MyPhoneNumber = tMgr.getSubscriberId();
    

  2. Or we can have a SMS Gateway, and whenever we need the phone number, we can send an sms to the gateway, and then deploy a webservice to return the number, the sms gateway receive the message, however this solution is costly.

这篇关于读取设备的电话号码将引发NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆