如何在Intent中发送Toast?

3

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

问题是当我显示toast提示时,应用程序会关闭。在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显示什么? - Raghav Sood
我该如何在Eclipse中逐行调试?我是否需要在所有行上设置检查点? - PlugInBoy
1
我的第一次尝试将捕获异常的PackageManager.NameNotFoundException更改为Exception,并确保在应用程序未安装时该方法不返回true。 - HericDenis
你只需要在想要断开代码的行的左侧单击(可能是双击),你可能需要设置一个属性,如debugable或其他我不记得的东西,但我认为eclipse会为您完成这些操作,并提示进行设置。 - HericDenis
看起来在这两种情况下(无论包是否已安装),您都已经安装了。另外,您可以尝试调用getInstalledApplications并遍历列表以确定包是否已安装。 - Durairaj Packirisamy
显示剩余9条评论
1个回答

0

你可以尝试使用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不是最终问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接