本文整理汇总了Java中android.content.pm.PackageManager类的典型用法代码示例。如果您正苦于以下问题:Java PackageManager类的具体用法?Java PackageManager怎么用?Java PackageManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PackageManager类属于android.content.pm包,在下文中一共展示了PackageManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: askForPermissons
import android.content.pm.PackageManager; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.M)
private void askForPermissons() {
Activity activity = getParentActivity();
if (activity == null) {
return;
}
ArrayList<String> permissons = new ArrayList<>();
if (activity.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
permissons.add(Manifest.permission.READ_CONTACTS);
permissons.add(Manifest.permission.WRITE_CONTACTS);
permissons.add(Manifest.permission.GET_ACCOUNTS);
}
if (activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissons.add(Manifest.permission.READ_EXTERNAL_STORAGE);
permissons.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
String[] items = permissons.toArray(new String[permissons.size()]);
activity.requestPermissions(items, 1);
}
示例2: requestPermissionIfNeeded
import android.content.pm.PackageManager; //导入依赖的package包/类
private static boolean requestPermissionIfNeeded(String permission, Activity activity) {
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, 1);
return true;
}
return false;
}
示例3: deliverPermissionResult
import android.content.pm.PackageManager; //导入依赖的package包/类
private static void deliverPermissionResult(CordovaPlugin plugin, int requestCode, String[] permissions) {
// Generate the request results
int[] requestResults = new int[permissions.length];
Arrays.fill(requestResults, PackageManager.PERMISSION_GRANTED);
try {
Method onRequestPermissionResult = CordovaPlugin.class.getDeclaredMethod(
"onRequestPermissionResult", int.class, String[].class, int[].class);
onRequestPermissionResult.invoke(plugin, requestCode, permissions, requestResults);
} catch (NoSuchMethodException noSuchMethodException) {
// Should never be caught since the plugin must be written for cordova-android 5.0.0+ if it
// made it to this point
LOG.e(LOG_TAG, "NoSuchMethodException when delivering permissions results", noSuchMethodException);
} catch (IllegalAccessException illegalAccessException) {
// Should never be caught; this is a public method
LOG.e(LOG_TAG, "IllegalAccessException when delivering permissions results", illegalAccessException);
} catch(InvocationTargetException invocationTargetException) {
// This method may throw a JSONException. We are just duplicating cordova-android's
// exception handling behavior here; all it does is log the exception in CordovaActivity,
// print the stacktrace, and ignore it
LOG.e(LOG_TAG, "InvocationTargetException when delivering permissions results", invocationTargetException);
}
}
示例4: getLocationPermission
import android.content.pm.PackageManager; //导入依赖的package包/类
private void getLocationPermission() {
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
示例5: getAppSource
import android.content.pm.PackageManager; //导入依赖的package包/类
/**
* 获取渠道,用于打包
*
* @param context
* @param metaName
* @return
*/
public static String getAppSource(Context context, String metaName) {
String result = null;
try {
ApplicationInfo appInfo = context.getPackageManager()
.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
if (appInfo.metaData != null) {
result = appInfo.metaData.getString(metaName);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
JLog.e(e.toString());
}
return result;
}
示例6: onAttachClick
import android.content.pm.PackageManager; //导入依赖的package包/类
public void onAttachClick() {
final int permissionStatus = ContextCompat.checkSelfPermission(
getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE
);
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_ATTACH_PERMISSION
);
return;
}
final Intent attach = new Intent(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("*/*");
startActivityForResult(attach, REQUEST_ATTACH_FILE);
}
开发者ID:PacktPublishing,项目名称:Hands-On-Android-UI-Development,代码行数:23,代码来源:AttachmentPagerFragment.java
示例7: beforeInvoke
import android.content.pm.PackageManager; //导入依赖的package包/类
@Override
protected boolean beforeInvoke(Object receiver, Method method, Object[] args) throws Throwable {
//API 2.3, 4.01, 4.0.3_r1,
/* public int getApplicationEnabledSetting(String packageName) throws RemoteException;*/
//API 4.1.1_r1, 4.2_r1, 4.3_r1, 4.4_r1, 5.0.2_r1
/*public int getApplicationEnabledSetting(String packageName, int userId) throws RemoteException;*/
if (args != null) {
final int index = 0;
if (args.length > index && args[index] instanceof String) {
String packageName = (String) args[index];
if (PluginManager.getInstance().isPluginPackage(packageName)) {
//DO NOTHING
setFakedResult(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
return true;
}
}
}
return super.beforeInvoke(receiver, method, args);
}
示例8: onNext
import android.content.pm.PackageManager; //导入依赖的package包/类
@Override
public void onNext(DownloadLink[] downloadLinks) {
String[] strings = new String[downloadLinks.length];
for (int i = 0; i < downloadLinks.length; i++) {
strings[i] = downloadLinks[i].getName();
}
new AlertDialog.Builder(this).setItems(strings, (dialogInterface, i) -> {
mSelectedLink = downloadLinks[i];
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED) {
loadMovie();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_PLAY_MOVIE);
}
}).show();
}
示例9: checkPermissions
import android.content.pm.PackageManager; //导入依赖的package包/类
private void checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Android M Permission check
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.location_access_title);
builder.setMessage(R.string.location_access_message);
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
public void onDismiss(DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.show();
}
}
}
示例10: getKeyHash
import android.content.pm.PackageManager; //导入依赖的package包/类
public static String getKeyHash(final Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_SIGNATURES);
if (packageInfo == null)
return null;
for (Signature signature : packageInfo.signatures) {
try {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
return android.util.Base64.encodeToString(md.digest(), android.util.Base64.NO_WRAP);
} catch (NoSuchAlgorithmException e) {
//Log.w(TAG, "Unable to get MessageDigest. signature=" + signature, e);
}
}
return null;
}
示例11: detect
import android.content.pm.PackageManager; //导入依赖的package包/类
public static int detect(Context c) {
if (Build.VERSION.SDK_INT < 5)
return APPSTORE;
PackageManager pm = c.getPackageManager();
String installer = pm.getInstallerPackageName(c.getPackageName());
if (installer != null && installer.equals("com.android.vending"))
return MARKET;
if (Build.MODEL.equalsIgnoreCase("Kindle Fire"))
return APPSTORE;
try {
if (pm.getPackageInfo("com.amazon.venezia", 0) != null)
return APPSTORE;
} catch (NameNotFoundException e) {
}
return MARKET;
}
示例12: newResult
import android.content.pm.PackageManager; //导入依赖的package包/类
@Override
protected ResolveInfo newResult(VPackage.ServiceIntentInfo filter, int match, int userId) {
final VPackage.ServiceComponent service = filter.service;
PackageSetting ps = (PackageSetting) service.owner.mExtras;
ServiceInfo si = PackageParserEx.generateServiceInfo(service, mFlags, ps.readUserState(userId), userId);
if (si == null) {
return null;
}
final ResolveInfo res = new ResolveInfo();
res.serviceInfo = si;
if ((mFlags & PackageManager.GET_RESOLVED_FILTER) != 0) {
res.filter = filter.filter;
}
res.priority = filter.filter.getPriority();
res.preferredOrder = service.owner.mPreferredOrder;
res.match = match;
res.isDefault = filter.hasDefault;
res.labelRes = filter.labelRes;
res.nonLocalizedLabel = filter.nonLocalizedLabel;
res.icon = filter.icon;
return res;
}
示例13: loadPackagePathIcon
import android.content.pm.PackageManager; //导入依赖的package包/类
public static Drawable loadPackagePathIcon(Context context, String path, String mimeType){
int icon = sMimeIcons.get(mimeType);
if (path != null) {
final PackageManager pm = context.getPackageManager();
try {
final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
if (packageInfo != null) {
packageInfo.applicationInfo.sourceDir = packageInfo.applicationInfo.publicSourceDir = path;
// know issue with nine patch image instead of drawable
return pm.getApplicationIcon(packageInfo.applicationInfo);
}
} catch (Exception e) {
return ContextCompat.getDrawable(context, icon);
}
} else {
return ContextCompat.getDrawable(context, icon);
}
return null;
}
示例14: loadAppList
import android.content.pm.PackageManager; //导入依赖的package包/类
public static List<ApplicationInfo> loadAppList(Context context) {
List<ApplicationInfo> applicationInfoList = context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
Collections.sort(applicationInfoList, new ApplicationInfo.DisplayNameComparator(context.getPackageManager()));
final ArrayList<ApplicationInfo> filteredApplicationInfoList = new ArrayList<>();
for (ApplicationInfo applicationInfo : applicationInfoList) {
if (context.getPackageManager().getLaunchIntentForPackage(applicationInfo.packageName) == null
|| applicationInfo.packageName.equals(context.getPackageName())) {
continue;
}
filteredApplicationInfoList.add(applicationInfo);
}
return filteredApplicationInfoList;
}
示例15: dispatchRequestPermissionsResultToFragment
import android.content.pm.PackageManager; //导入依赖的package包/类
private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Fragment fragment) {
// If the package installer crashed we may have not data - best effort.
String[] permissions = (data != null) ? data.getStringArrayExtra(
PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
final int[] grantResults = (data != null) ? data.getIntArrayExtra(
PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
}