2012-03-07 58 views

回答

0

如果您想首先在您的Android應用程序上分享Facebook或Twitter上的內容,則必須先將您的應用程序與Android SDK和Twitter SDK集成。如果你想分享這些bethout整合這些,那麼你可以通過簡單地發送意圖像如下做到這一點...

public class MainActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // listeners of our two buttons 
    View.OnClickListener handler = new View.OnClickListener() { 
     public void onClick(View v) { 
      switch (v.getId()) { 

      case R.id.buttonShareTextUrl: 
       shareTextUrl(); 
       break; 

      case R.id.buttonShareImage: 
       shareImage(); 
       break; 
      } 
     } 
    }; 

    // our buttons 
    findViewById(R.id.buttonShareTextUrl).setOnClickListener(handler); 
    findViewById(R.id.buttonShareImage).setOnClickListener(handler); 

} 

/* 
* Method to share either text or URL. 
*/ 
private void shareTextUrl() { 
    Intent share = new Intent(android.content.Intent.ACTION_SEND); 
    share.setType("text/plain"); 
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 

    // Add data to the intent, the receiving app will decide 
    // what to do with it. 
    share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post"); 
    share.putExtra(Intent.EXTRA_TEXT, "http://two55am.com"); 

    startActivity(Intent.createChooser(share, "Share link!")); 
}  

/* 
* Method to share any image. 
*/ 
private void shareImage() { 
    Intent share = new Intent(Intent.ACTION_SEND); 

    // If you want to share a png image only, you can do: 
    // setType("image/png"); OR for jpeg: setType("image/jpeg"); 
    share.setType("image/*"); 

    // Make sure you put example png image named myImage.png in your 
    // directory 
    String imagePath = Environment.getExternalStorageDirectory() 
      + "/twitter.png"; 

    File imageFileToShare = new File(imagePath); 

    Uri uri = Uri.fromFile(imageFileToShare); 
    share.putExtra(Intent.EXTRA_STREAM, uri); 

    startActivity(Intent.createChooser(share, "Share Image!")); 
} 

} 

注意,你的Facebook和Twitter的apk應安裝在您的設備/模擬器上面的代碼工作。

+0

Facebook不解析EXTRA_SUBJECT。 – user1 2013-06-17 15:38:38