查找是否安装了应用程序并显示对话框

时间:2017-01-03 04:47:25

标签: java android dialog

如果没有安装点击按钮后如何安装检查应用程序如何从谷歌播放显示对话框下载应用程序 Java / android

2 个答案:

答案 0 :(得分:0)

这是方法。

private boolean isPackageInstalled(String packagename) {
    PackageInfo info = null;
    boolean isInstalled = false;
    PackageManager pm = getPackageManager();
    try {
        info = pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        isInstalled = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return isInstalled;
} 

此方法将返回true或false。您可以按如下方式调用它。

if(isPackageInstalled("Your package name")) {
     /*Perform task when package is installed*/
}

答案 1 :(得分:-1)

public class Sample extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Put the package name here...
        boolean installed = appInstalledOrNot("com.Ch.Example.pack");  
        if(installed) {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.Ch.Example.pack");
            startActivity(LaunchIntent);

            System.out.println("App is already installed on your phone");         
        } else {
            System.out.println("App is not currently installed on your phone");
        }
    }

    private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }

        return false;
    }

}