【问题标题】:That way I can send a Toast within Intent?这样我可以在 Intent 内发送 Toast 吗?
【发布时间】:2012-12-04 15:09:50
【问题描述】:

我的应用程序中有 4 个按钮,在其中 2 个按钮中,我需要检查我要运行的应用程序是否在我的设备中。如果存在,则运行,否则,显示敬酒。

问题是当我展示祝酒词时,应用程序关闭。在 startActivity(Intent) 中显示 Toast 的最佳方式是什么???

这是代码:

public void onClick(View v) {

    Intent LaunchIntent = null;
    boolean isTouch = isAppInstalled("com.enflick.android.ping");
    boolean isWpress = isAppInstalled("org.wordpress.android");


    switch (v.getId()) {
    case (R.id.botonPortafoli):
        LaunchIntent = new Intent(this, PortafoliActivity.class);
        break;
    case (R.id.botonSocial):
        LaunchIntent = new Intent(this, SocialActivity.class);
        break;

    case (R.id.botonTouch):

        if (isTouch) {
            LaunchIntent = getPackageManager().getLaunchIntentForPackage(
                    "com.enflick.android.ping");

        } else {
            Toast.makeText(this, R.string.no_inst, Toast.LENGTH_LONG)
                    .show();

            break;
        }

        break;
    case (R.id.botonWpress):

        if (isWpress) {
            LaunchIntent = getPackageManager().getLaunchIntentForPackage(
                    "org.wordpress.android");

            break;

        } else {

            Toast.makeText(this, R.string.no_inst, Toast.LENGTH_LONG)
            .show();


            break;
        }

    }
startActivity(LaunchIntent);

【问题讨论】:

  • 应用中断是什么意思? LogCat 说什么?
  • 您是否尝试逐行调试以查看其中断的位置?
  • 如何在eclipse中逐行调试?我可能会在所有行中都添加检查点??
  • 我的第一次尝试是将捕获异常 PackageManager.NameNotFoundException 更改为 Exception,并确保在未安装应用程序时该方法不返回 true。
  • 你只需要左键单击(可能是双击)你想要破解代码的行的左边,你可能需要设置一个属性,比如 debugable 或者我不记得的东西,但我认为 eclipse 会为你做这件事,只是要求去做。

标签: android packages


【解决方案1】:

你可以用 try/catch 来代替isAppInstalled("com.enflick.android.ping")。我认为您无法在不实际启动应用程序的情况下检查应用程序是否已安装...

public void onClick(View v) {

    Intent LaunchIntent = new Intent();

    if (v.getId() == (R.id.botonPortafoli)) {
        LaunchIntent = new Intent(this, PortafoliActivity.class);
        startActivity(LaunchIntent);
    } else if (v.getId() == (R.id.botonSocial)) {
        LaunchIntent = new Intent(this, SocialActivity.class);
        startActivity(LaunchIntent);
    } else if (v.getId() == (R.id.botonTouch)) {
        try {
            LaunchIntent = getPackageManager().getLaunchIntentForPackage(
                    "com.enflick.android.ping");
            startActivity(LaunchIntent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, R.string.no_inst, Toast.LENGTH_LONG).show();
        }
    } else if (v.getId() == (R.id.botonWpress)) {
        try {
            LaunchIntent = getPackageManager().getLaunchIntentForPackage(
                    "org.wordpress.android");
            startActivity(LaunchIntent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, R.string.no_inst, Toast.LENGTH_LONG).show();
        }
    }
}

希望这对你有用,当我试图验证官方 Twitter 应用程序是否安装在设备上时,它对我有用。

顺便说一句,为了安全起见,最好使用if-else 而不是switch 以避免资源ID 不是最终问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多