PackageUtils.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.zswc.creativity.utils;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.pm.PackageInfo;
  5. import android.content.pm.PackageManager;
  6. import android.net.Uri;
  7. import android.provider.Settings;
  8. import com.zswc.creativity.App;
  9. public class PackageUtils {
  10. /**
  11. * 检查微信是否安装
  12. *
  13. * @param context
  14. *
  15. * @return
  16. */
  17. public static boolean checkAppInstalled(Context context, String packageName) {
  18. PackageManager packageManager = context.getPackageManager();
  19. try {
  20. PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
  21. if (packageInfo != null) {
  22. return true;
  23. }
  24. } catch (PackageManager.NameNotFoundException e) {
  25. e.printStackTrace();
  26. }
  27. return false;
  28. }
  29. /**
  30. * 卸载指定包名的应用
  31. *
  32. * @param packageName
  33. */
  34. public static boolean uninstall(Context context, String packageName) {
  35. boolean b = checkAppInstalled(context, packageName);
  36. if (b) {
  37. Uri packageURI = Uri.parse("package:".concat(packageName));
  38. Intent intent = new Intent(Intent.ACTION_DELETE);
  39. intent.setData(packageURI);
  40. context.startActivity(intent);
  41. return true;
  42. }
  43. return false;
  44. }
  45. /**
  46. * 跳转到指定包名的应用信息页
  47. *
  48. * @param packageName
  49. */
  50. public static boolean appInfo(Context context, String packageName) {
  51. boolean b = checkAppInstalled(context, packageName);
  52. if (b) {
  53. Uri packageURI = Uri.parse("package:".concat(packageName));
  54. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  55. intent.setData(packageURI);
  56. context.startActivity(intent);
  57. return true;
  58. }
  59. return false;
  60. }
  61. // public static void openApp(Context context, String packageName) {
  62. // Intent intent = App.getApp().getPackageManager().getLaunchIntentForPackage(packageName);
  63. // if (intent != null) {
  64. // context.startActivity(intent);
  65. // }
  66. // }
  67. }