Advertisement
retnet

jika app ada atau tidak

May 13th, 2022
869
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Function 1
  2.  
  3. private boolean isAppInstalled(String packageName) {
  4.     PackageManager pm = getPackageManager();
  5.     boolean installed = false;
  6.     try {
  7.         pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
  8.         installed = true;
  9.     } catch (PackageManager.NameNotFoundException e) {
  10.         installed = false;
  11.     }
  12.     return installed;
  13. }
  14.  
  15. //Or Function 2
  16.  
  17. public boolean isAppInstalled(String targetPackage){
  18.     List<ApplicationInfo> packages;
  19.     PackageManager pm = getPackageManager();        
  20.     packages = pm.getInstalledApplications(0);
  21.     for (ApplicationInfo packageInfo : packages) {
  22.         if(packageInfo.packageName.equals(targetPackage)) return true;
  23.     }        
  24.     return false;
  25. }
  26.  
  27. //USAGE
  28.  
  29. if(isAppInstalled("com.package.name")){
  30.     //Your Code
  31. }
  32. else{
  33.     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.package.name")));
  34. }
  35.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement