Android sends direct Twitter message through intent

I try to open Twitter from my application and when Twitter opens, to show a direct message window with information about the username and text (message), so the user of the application only needs to read the message, change it if he wants and click "Send".

I have the following code that works for composing a tweet . If Twitter is not installed, it will try to open twitter from a web browser. The missing part is for preparing a direct message from your own Twitter application.

    try{
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, "It 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!

+4
source share
1 answer
 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();
    }
+2

All Articles