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

ありがとう!

4

1 に答える 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();
    }
于 2015-09-22T06:00:44.770 に答える