getSystemService方法的调用(转http://blog.csdn.net/bianhaohui/article/details/6220135)

Coordinator
Apr 16, 2012 at 7:37 AM

2011-03-03 11:11 2224人阅读 评论(0) 收藏 举报

  在android 获取手机信息的时候用到这样一段代码:

 

 

public class BasicInfo {

 

 

public String getPhoneNumber()

{

// 获取手机号 MSISDN,很可能为空

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

StringBuffer inf = new StringBuffer();

switch(tm.getSimState()){ //getSimState()取得sim的状态  有下面6中状态  

        case TelephonyManager.SIM_STATE_ABSENT :inf.append("无卡");return inf.toString();   

        case TelephonyManager.SIM_STATE_UNKNOWN :inf.append("未知状态");return inf.toString();  

        case TelephonyManager.SIM_STATE_NETWORK_LOCKED :inf.append("需要NetworkPIN解锁");return inf.toString();  

        case TelephonyManager.SIM_STATE_PIN_REQUIRED :inf.append("需要PIN解锁");return inf.toString();  

        case TelephonyManager.SIM_STATE_PUK_REQUIRED :inf.append("需要PUK解锁");return inf.toString();  

        case TelephonyManager.SIM_STATE_READY :break;  

        }

 

String phoneNumber = tm.getLine1Number();

return phoneNumber;

}

 

 

......

 

}

 

在另外一个activity类里面调用的时候 总是出现进程关闭 无法获取手机信息。

 

后来发现

getSystemService这个方法基于context,只有存在TextView控件的窗体中这个方法才会被激活~

 

于是:

1.

给BasicInfo 添加一个带参数Context的构造函数:

public BasicInfo (Context context)

{

this.context = context;

}

 

2.

getPhoneNumber()函数里面改成:

context.getSystemService(Context.TELEPHONY_SERVIC);

 

3.

在调用类里面 BasicInfo bi = new BasicInfo(this);

bi.getPhoneNumber();

 ==================================================

TelephonyManager localTelephonyManager = (TelephonyManager)getSystemService("phone");
      Constant.IMSI = localTelephonyManager.getSubscriberId();
      Constant.IMEI = localTelephonyManager.getDeviceId();
      if (this.openCommonUsed)
        this.often_btn.performClick();
      if (this.openAllUsed)
        this.all_btn.performClick();
      return;
      this.globTask = new GetGlobalEyeListByTypeAndAreaTask(this, this, 2);
      GetGlobalEyeListByTypeAndAreaTask localGetGlobalEyeListByTypeAndAreaTask1 = this.globTask;
      Object[] arrayOfObject1 = new Object[1];
      arrayOfObject1[0] = localRequestEyeEntity;
      localGetGlobalEyeListByTypeAndAreaTask1.execute(arrayOfObject1);