检测Android系统是否安装了某个应用,该方法如下:
public boolean hasApplication(Context context, String packageName){
PackageManager packageManager = context.getPackageManager();
//获取系统中安装的应用包的信息
List<PackageInfo> listPackageInfo = packageManager.getInstalledPackages(0);
for (int i = 0; i < listPackageInfo.size(); i++) {
if(listPackageInfo.get(i).packageName.equalsIgnoreCase(packageName)){
return true;
}
}
return false;
}
如何去启动上述的检测方法,可以在某个Button的点击事件中添加如下代码:
if(hasApplication(this, "com.qq.tt")){
Intent i = new Intent();
//参数1:要跳转到的应用的包名 参数二:该包中的某个Activity
ComponentName cn = new ComponentName("com.qq.tt",
"com.qq.tt.MainActivity");
i.setComponent(cn);
startActivity(i);
} else {
Toast.makeText(this, "不存在该应用", Toast.LENGTH_LONG).show();
}
此处需要格外说明:
假设需要从a应用跳转到b应用,
1.如果为显式跳转(Intent.setClass(this,Activity.class)),那么b应用显示的窗口应该设置为<activity android:exported="true">;
2.如果为隐式跳转(Intent.setAction(" ")),则Intent的设置的Action必须在b应用窗口的<Intent-filter>中有对应的<action />;