2013-11-27 61 views
4

我试图从我的应用中打开Twitter,当twitter打开时,显示带有用户名信息和一些文本(消息)的Direct Message窗口,因此应用的用户只需阅读该消息,如果他想修改它并点击发送。我有以下代码正在撰写鸣叫。如果Twitter应用程序未安装,它会尝试从Web浏览器中打开Twitter。 缺少的部分是从原生Twitter应用程序准备直接消息。Android通过intent发送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))) 
} 

谢谢!

回答

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

我应该在哪里得到follower_uid –