4

I'm trying to open Twitter from my app and when twitter opens, to show the Direct Message window with the username info and some text (message), so the app's user only have to read the message, modify it if he wants and to tap send.

I have the following code that is working to compose a tweet. If the Twitter app is not installed it would try to open twitter from the web browser. The missing part is to prepare the Direct Message from native Twitter App.

    try{
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, "It's a Tweet!" + "#MyApp");
    intent.setType("text/plain");
    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);
        Log.i("APP",app.activityInfo.name);
        if ("com.twitter.applib.PostActivity".equals(app.activityInfo.name)) {
            final ActivityInfo activity=app.activityInfo;
                final ComponentName x=new ComponentName(activity.applicationInfo.packageName, activity.name);
                intent=new Intent(Intent.ACTION_SEND);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                intent.setComponent(x);
                intent.putExtra(Intent.EXTRA_TEXT, "blah blah" );
                startActivity(intent);
                break;
        }
    }
} catch(final ActivityNotFoundException e) {
    Log.i("Twitter intent", "no twitter native", e );
    String usernameWeb="https://twitter.com/direct_messages/create/"+user;
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(usernameWeb)))
}

Thanks!

1 Answers1

3
 try {
        Intent openNewIntent = new Intent();
        String mPackage = "com.twitter.android";
        String mClass = ".DMActivity";
        openNewIntent.setComponent(new ComponentName(mPackage,mPackage+mClass));
        openNewIntent.putExtra("user_ids", new long[]{follower_uid});
        openNewIntent.putExtra("keyboard_open", true);
        startActivity(  openNewIntent);
    } catch (Exception e) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/direct_messages/create/" + screen_name)));
        //e.printStackTrace();
    }
DJMacross
  • 221
  • 3
  • 5