Example usage for android.content Context checkCallingPermission

List of usage examples for android.content Context checkCallingPermission

Introduction

In this page you can find the example usage for android.content Context checkCallingPermission.

Prototype

@CheckResult(suggest = "#enforceCallingPermission(String,String)")
@PackageManager.PermissionResult
public abstract int checkCallingPermission(@NonNull String permission);

Source Link

Document

Determine whether the calling process of an IPC you are handling has been granted a particular permission.

Usage

From source file:Main.java

private static boolean hasExternalStoragePermission(Context context) {
    int permission = context.checkCallingPermission(EXTERNAL_STORAGE_PERMISSION);
    return permission == PackageManager.PERMISSION_GRANTED;
}

From source file:org.cocos2dx.lib.CCUtils.java

public static String getMacAddress() {
    String mac = "";
    Context ctx = Cocos2dxActivity.getContext();

    // first, try to get mac from wifi manager
    if (ctx.checkCallingPermission(permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED) {
        WifiManager wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        mac = info.getMacAddress();/*ww w.  j  a va 2 s.c o  m*/
    }

    // if failed, try from network interface api
    if (TextUtils.isEmpty(mac)) {
        if (Build.VERSION.SDK_INT >= 9) {
            try {
                NetworkInterface ne = NetworkInterface
                        .getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
                byte[] b = ne.getHardwareAddress();
                mac = byte2Hex(b);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // if failed, use fake
    if (TextUtils.isEmpty(mac)) {
        mac = "00:00:00:00:00:00";
    }

    // return
    return mac;
}