2014-01-13 6 views
13

私はtwitterのインテントを起動するために以下のコードを使用しますが、動作しません。私は私の携帯電話にインストールされているTwitterアプリを持っている。助けが必要!Androidの起動Twitterの意図

 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
     shareIntent.setType("text/plain"); 
     shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share"); 
     PackageManager pm = contexto.getPackageManager(); 
     List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0); 
     for (final ResolveInfo app : activityList) { 
      if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) { 
       final ActivityInfo activity = app.activityInfo; 
       final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name); 
       shareIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
       shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
       shareIntent.setComponent(name); 
       contexto.startActivity(shareIntent); 
       break; 
      } 
     } 

私が活動を呼び出すしようとすると、例外を取得:テント

Intent tweetIntent = new Intent(Intent.ACTION_SEND); 
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test."); 
tweetIntent.setType("text/plain"); 

PackageManager packManager = getPackageManager(); 
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY); 

boolean resolved = false; 
for(ResolveInfo resolveInfo: resolvedInfoList){ 
    if(resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")){ 
     tweetIntent.setClassName(
      resolveInfo.activityInfo.packageName, 
      resolveInfo.activityInfo.name); 
     resolved = true; 
     break; 
    } 
} 
if(resolved){ 
    startActivity(tweetIntent); 
}else{ 
    Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show(); 
} 
+0

あなたはどんなエラー/例外を取得していますか?あなたのコードをキャッチして試してみて、何らかのエラーがあるかどうか確認してください。 – Dev01

+0

android.content.ActivityNotFoundException:明示的なアクティビティクラス{com.twitter.android/com.twitter.android.PostActivity}を見つけることができません。あなたのAndroidManifest.xmlでこのアクティビティを宣言しましたか?私はアンドロイドのマニフェストに何かを置く必要がありますか? – user3065901

答えて

43

をTaranfxのおかげで)使用Rのフィードの意図(変化のuser_id =>SCREEN_NAME):

public static void startTwitter(Context context) { 

    Intent intent = null; 
    try { 
     // get the Twitter app if possible 
     context.getPackageManager().getPackageInfo("com.twitter.android", 0); 
     intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=<place_user_name_here>")); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     return intent; 
    } catch (Exception e) { 
     // no Twitter app, revert to browser 
     intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/<place_user_name_here>")); 
    } 
    startActivity(intent); 
} 
+1

+1いいアイデア..... – Dev01

+0

ありがとうございました。それは(少し微調整して)うまくいった – TheOnlyAnil

4

少し修正(郵便の場合は、ユーザーのフィード

Intent intent = null; 
try { 
    // get the Twitter app if possible 
    this.getPackageManager().getPackageInfo("com.twitter.android", 0); 
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID")); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
} catch (Exception e) { 
    // no Twitter app, revert to browser 
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERID_OR_PROFILENAME")); 
} 
this.startActivity(intent); 

を起動するための一般的

 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.twitter.android/com.twitter.android.PostActivity}; have you declared this activity in your AndroidManifest.xml?