在我的活动开始之前需要一个警报对话框

时间:2012-06-20 16:52:45

标签: android android-intent alert

这就是我现在所拥有的。正在检查是否安装了GoLauncher,如果是,则将其带到go启动器主屏幕。如果未安装,则需要用户将其安装到市场上。

我需要的是,如果已安装,我需要弹出一个警告框,显示用户如何安装主题。在用户点击之后,它应该转到GoLaunhcer主屏幕

gosetting = (ImageButton) findViewById(R.id.gosetting);
    gosetting.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setComponent(new ComponentName("com.gau.go.launcherex","com.jiubang.ggheart.apps.desks.diy.GoLauncher"));
                startActivity(intent);
            } 
            catch (ActivityNotFoundException e) {
                AlertDialog.Builder alert = new AlertDialog.Builder(HelperActivity.this);
                alert.setTitle("GO Not Found");
                alert.setMessage("Do you want to vist the GO Launcher Android Market page?");
                alert.setIcon(R.drawable.go_icon2);
                alert.setPositiveButton("Yes",
                 new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                      final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("market://details?id=com.gau.go.launcherex"));
                      startActivity(intent);
                  }
                 });
                alert.setNegativeButton("No",
                 new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                      return;
                  }
                 });

                alert.show();

            } catch (Exception go) {
                go.printStackTrace();
            }
        }

    });

添加了您更新的代码,现在收到此错误.. !1

1 个答案:

答案 0 :(得分:0)

查看Detect an application is installed or not?的答案解释了如何查询PackageManager以确定您的应用程序是否已安装。

编辑:添加代码

public void onClick(View v) {
    if (isAppInstalled("com.gau.go.launcherex")) {
        // Show dialog about installing the theme
        AlertDialog.Builder alert = new AlertDialog.Builder(HelperActivity.this);
        alert.setTitle("GO Theme installer");
        alert.setMessage("You need to install the theme like this...");
        alert.setIcon(R.drawable.go_icon2);
        alert.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  // Here actually start the GoLauncher
                  final Intent intent = new Intent(Intent.ACTION_MAIN);
                  intent.setComponent(new ComponentName("com.gau.go.launcherex","com.jiubang.ggheart.apps.desks.diy.GoLauncher"));
                  startActivity(intent);
              }
             });
        alert.show();
    } else {
        // Here you put up the dialog about visitng the market page
        AlertDialog.Builder alert = ...
    }
}

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