twitter没有发现活动异常

时间:2012-10-24 07:48:26

标签: android exception

我有以下代码从我的应用中发推特:

try {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT,
                    "Blah blah blah");
            final PackageManager pm = getPackageManager();
            final List<?> activityList = pm
                    .queryIntentActivities(intent, 0);
            int len = activityList.size();
            for (int i = 0; i < len; i++) {
                final ResolveInfo app = (ResolveInfo) activityList.get(i);
                if ("com.twitter.android.PostActivity"
                        .equals(app.activityInfo.name)) {
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.setClassName("com.twitter.android",
                            "com.twitter.android.PostActivity");

                    startActivity(intent);
                    break;
                }
            }
        } catch (final ActivityNotFoundException e) {
            onCreateDialog(NO_APP);
        }

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch (id) {

    case NO_APP: {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.icon_small);
        builder.setTitle("Twitter");
        builder.setMessage("No Twitter app found");
        builder.setCancelable(false);
        builder.setPositiveButton("OK", new Dialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                finish();
            }
        });

        dialog = builder.create();
        return dialog;
    }
    default:
        return super.onCreateDialog(id);
    }
}

如果我的手机上安装了Twitter,那么该应用程序可以正常工作并生成新的推文。如果该应用程序不存在,我想要一个对话框,显示没有应用程序。但是,到目前为止,手机中没有推特应用程序时没有任何反应。当我按下我的应用程序上的推文按钮时,没有任何反应。在这种情况下,我需要做什么才能使对话框出现?

1 个答案:

答案 0 :(得分:1)

如果找不到Twitter应用程序,则没有例外。 将代码更改为:

try {
boolean found = false;
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT,
                    "Blah blah blah");
            final PackageManager pm = getPackageManager();
            final List<?> activityList = pm
                    .queryIntentActivities(intent, 0);
            int len = activityList.size();
            for (int i = 0; i < len; i++) {
                final ResolveInfo app = (ResolveInfo) activityList.get(i);
                if ("com.twitter.android.PostActivity"
                        .equals(app.activityInfo.name)) {
found = true;
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.setClassName("com.twitter.android",
                            "com.twitter.android.PostActivity");

                    startActivity(intent);
                    break;
                }
            }
if(!found)  onCreateDialog(NO_APP);
        } catch (final ActivityNotFoundException e) {
            onCreateDialog(NO_APP);
        }

或者尝试启动twitter应用程序并处理未找到的异常。

try {

            Intent intent = new Intent();
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
            startActivity(intent);

        } catch (final ActivityNotFoundException e) {
            onCreateDialog(NO_APP);
        }