Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class Main {

    public static String getPhoneNumber(Context context) {
        try {
            String phone = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
                    .getLine1Number();
            if (!TextUtils.isEmpty(phone) && phone.startsWith("+86")) {
                phone = phone.substring(3);
            }
            return phone;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}