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

4

我想在我的应用程序中打开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个回答

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

1
我应该从哪里获取follower_uid? - Binil Surendran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接