自动获取sim号码填入edittext

// 获取手机内sim卡电话号码
		TelephonyManager tManager=(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
		
		if(isSimReady() && "".equals(et_input_cellnum_design.getText().toString().trim())){
			// 有电话卡 有电话号码
			phoneNum = tManager.getLine1Number()==null?"":tManager.getLine1Number()+"";
			System.out.println("获取手机sim卡里面的号码:【"+phoneNum+"】");
			if(phoneNum.contains("+86")){
				phoneNum = phoneNum.substring(3);
			}
			et_input_cellnum_design.setText(phoneNum);
			
			System.out.println("ChooseDecorateDesignFragment 默认获取手机sim卡的电话号码" + phoneNum);
		}else{
			phoneNum = et_input_cellnum_design.getText().toString().trim();
		}


/**
	 * 手机是否有sim卡
	 * @return
	 */
	private boolean isSimReady(){
		TelephonyManager tManager=(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
		int simState = tManager.getSimState();
		boolean hasSim = false;
		switch (simState) {
		case TelephonyManager.SIM_STATE_ABSENT:
			hasSim = false;
			break;
		case TelephonyManager.SIM_STATE_UNKNOWN:
			hasSim = false;
			break;
		case TelephonyManager.SIM_STATE_READY:
			hasSim = true;
			break;
		}
		return hasSim;
	}


你可能感兴趣的:(android,java)