'androidtelephonymanager'에 해당되는 글 1건

  1. 2020.11.06 안드로이드 전화번호 가져오기
Android2020. 11. 6. 15:20

 

 

안드로이드 프로젝트에서 현재 기기의 전화번호가 필요할 때가 있다. 

TelephonyManager 객체를 이용해서 전화번호를 가져올 수 있다.

TelephonyManager 객체 접근하기 위해서는 Context 객체를 꼭 넘겨주어야 한다.

태블릿pc의 경우는 0이 나오게 되어있음.

 

    /**
     * 전화번호 가져오기
     * @param context
     * @return 전화번호
     */
    public static String getCpNum(Context context) {
        String num = "";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            num = telephonyManager.getLine1Number();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(Strings.isNullOrEmpty(num)) {
            return "0";
        } else {
            return num.replace("+82", "0");
        }
    }
Posted by 사슴영혼'-'