Ankush
Ankush

Reputation: 6927

Activity not found exception with twitter

I have the following code to tweet something from my app:

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);
    }
}

If twitter is installed on my phone, then the app works fine and a new tweet is generated. If the app does not exist, I want a dialog to appear stating that there is no app. However, as of now nothing happens when there is no twitter app in the phone. When I press the tweet button on my app, nothing happens. What do I need to do to make the dialog appear in this case?

Upvotes: 0

Views: 809

Answers (1)

Anis BEN NSIR
Anis BEN NSIR

Reputation: 2555

There is no exception if the twitter Application is not found. Change you code to:

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);
        }

Or Try to start the twitter application and handle the not found exception.

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);
        }

Upvotes: 1

Related Questions