Android通过意图发送Twitter直接消息

时间:2013-11-27 13:18:59

标签: android twitter dm

我试图从我的应用程序打开Twitter,当Twitter打开时,显示带有用户名信息和一些文本(消息)的直接消息窗口,因此应用程序的用户只需阅读消息,如果他想要修改它,并点击发送。

我有以下代码正在撰写推文。如果未安装Twitter应用程序,它将尝试从Web浏览器打开Twitter。 缺少的部分是从本地Twitter应用程序准备直接消息。

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

谢谢!

1 个答案:

答案 0 :(得分:2)

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