2011-08-01 36 views

Trả lời

6

Nếu người dùng đã có cài đặt Twitter trên điện thoại của họ, một cái gì đó như thế này nên chăm sóc nó:

try{ 
      Intent intent = new Intent(Intent.ACTION_SEND); 
      intent.putExtra(Intent.EXTRA_TEXT, "this is a tweet"); 
      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); 
       if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) { 
        final ActivityInfo activity=app.activityInfo; 
        final ComponentName name=new ComponentName(activity.applicationInfo.packageName, activity.name); 
        intent.addCategory(Intent.CATEGORY_LAUNCHER); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
        intent.setComponent(name); 
        startActivity(intent); 
        break; 
       } 
      } 
     } 
     catch(final ActivityNotFoundException e) { 
      Log.i("twitter", "no twitter native",e); 
     } 
+0

Bạn cần phải oauth ứng dụng của mình trước khi bạn có thể đăng ... –

+0

@Adam Storm, tôi nên tuyên bố rằng điều này chỉ hoạt động nếu người dùng đã cài đặt Twitter trên điện thoại của họ. – Phil

+0

yes trong trường hợp đó mã này có vẻ phù hợp với tôi =) –

0
private void showTwitter(){ 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setClassName("com.twitter.android", "com.twitter.android.ProfileActivity");  
     this.startActivity(intent);  
} 
8
try { 
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + twitter_user_name))); 
}catch (Exception e) { 
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + twitter_user_name))); 
} 

này nên làm công việc cần thiết cho bạn.

+0

câu trả lời của bạn hoạt động tốt khi ứng dụng được cài đặt, nhưng khi nó mở trình duyệt, nó không chuyển hướng đến tài khoản..có thể bạn giúp –

+0

nếu ứng dụng chưa được cài đặt sử dụng liên kết này startActivity (Mục đích mới (Intent.ACTION_VIEW, Uri.parse ("https://twitter.com/" + twitter_user_name))); không có #! phù hợp với tôi –

2

tôi sử dụng này:

Intent i = getOpenTwitterIntent(this, "UserName"); 
startActivity(i); 

Và chức năng:

public static Intent getOpenTwitterIntent(Context c, String Username) { 

     try { 
      c.getPackageManager().getPackageInfo("com.twitter.android", 0); 
      return new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name="+ Username)); 
     } catch (Exception e) { 
      return new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + Username)); 
     } 
    } 

Nếu Twitter ứng dụng được intalled trong thiết bị, điều này mở rằng, trình duyệt web khác mở ...

2

Mở rộng câu trả lời của @ Chrishan, bạn có thể mở ứng dụng twitter để thực hiện các chức năng khác nhau dựa trên uri (danh sách dưới đây từ post)

này
twitter://user?screen_name=lorenb 
twitter://user?id=12345 
twitter://status?id=12345 
twitter://timeline 
twitter://mentions 
twitter://messages 
twitter://list?screen_name=lorenb&slug=abcd 
twitter://post?message=hello world 
twitter://post?message=hello world&in_reply_to_status_id=12345 
twitter://search?query=%23hashtag 

ví dụ:

String uriStr = "twitter://post?message=hello world" 
try { 
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(uriStr))); 
}catch (Exception e) { 
    //the user doesn't have twitter installed 
} 

Chú ý: Tôi đã chỉ cố gắng userpost, vì vậy nếu bạn gặp bất kỳ mà không làm việc xin vui lòng cho tôi biết và tôi sẽ cập nhật câu trả lời này.

Các vấn đề liên quan