get Authority From Permission - Android App

Android examples for App:Package

Description

get Authority From Permission

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import java.util.List;

public class Main {

    public static String getAuthorityFromPermission(Context context,
            String permission) {//from   w w  w .ja  v  a2 s.  c  om
        if (context == null || permission == null) {
            return null;
        }

        List<PackageInfo> packageInfoList = context.getPackageManager()
                .getInstalledPackages(PackageManager.GET_PROVIDERS);

        if (packageInfoList != null) {
            for (PackageInfo packageInfo : packageInfoList) {
                ProviderInfo[] providerInfos = packageInfo.providers;
                if (providerInfos != null) {
                    for (ProviderInfo providerInfo : providerInfos) {
                        if (permission.equals(providerInfo.readPermission)
                                || permission
                                        .equals(providerInfo.writePermission)) {
                            return providerInfo.authority;
                        }
                    }
                }
            }
        }

        return null;
    }
}

Related Tutorials