Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;

import android.content.pm.PackageManager;

import android.telephony.TelephonyManager;

public class Main {
    static Context collectUtilContext;

    /**
     * @param context
     * @return String
     * @throws
     * @Title: getPhoneId
     * @Description: TODO
     */
    public static String getPhoneId(Context context) {
        if (context == null) {
            return "";
        }
        if (checkPermission(context, "android.permission.READ_PHONE_STATE")) {
            String phoneId = "";
            if (checkPhoneState(context)) {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                phoneId = tm.getDeviceId();
                if (phoneId != null) {
                    return phoneId;
                }
            }
        }
        return "";
    }

    /**
     * @param context
     * @param permission
     * @return boolean
     * @Title: checkPermission
     * @Description: TODO
     */
    public static boolean checkPermission(Context context, String permission) {

        collectUtilContext = context;
        PackageManager pm = context.getPackageManager();
        return pm.checkPermission(permission,
                collectUtilContext.getPackageName()) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * @param context
     * @return Boolean
     * @throws
     * @Title: checkPhoneState
     * @Description: TODO
     */
    private static Boolean checkPhoneState(Context context) {
        PackageManager pm = context.getPackageManager();
        if (pm.checkPermission("android.permission.READ_PHONE_STATE", context.getPackageName()) != 0) {
            return false;
        }
        return true;
    }
}