Android 判断程序是否已安装 & 启动另一个程序

需要用到的权限


 


首先是判断是否有安装某个程序

原文地址:http://hh-blog.appspot.com/2011/01/13/Android-installed.html

public boolean checkApkExist(Context context, String packageName) {
     if (packageName == null || “”.equals(packageName))
         return false;
    
     try {
         ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,PackageManager.GET_UNINSTALLED_PACKAGES);
         return true;
     } catch (NameNotFoundException e) {
         return false;
     }
 }

接下来是启动另外一个API程序

public static void RunActivity(String packetName, Context context)
	{
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(packetName, 0);
			Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);  
	        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
	        resolveIntent.setPackage(pi.packageName);  
	           
	        List apps = context.getPackageManager().queryIntentActivities(resolveIntent,0);
	        ResolveInfo ri = apps.iterator().next();
	        if (ri != null ) {  
	            String packageName1 = ri.activityInfo.packageName;  
	            String className = ri.activityInfo.name;  
	               
	            Intent intent = new Intent(Intent.ACTION_MAIN);  
	            intent.addCategory(Intent.CATEGORY_LAUNCHER);  
	               
	            ComponentName cn = new ComponentName(packageName1, className);  
	               
	            intent.setComponent(cn);  
	            context.startActivity(intent);  
	        }  
		} catch (NameNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}


你可能感兴趣的:(android)