别的不说了,直接上代码。
支持分享到微信、微博、facebook、twitter
package com.example.shareSample; import java.util.List; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { protected void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); setContentView(R.layout.main); ((Button) findViewById(R.id.toFacebookButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View paramAnonymousView) { Toast.makeText( MainActivity.this.getApplicationContext(), "waiting to share to facebook", Toast.LENGTH_SHORT).show(); MainActivity.this.shareToFacebook(); } }); ((Button) findViewById(R.id.toTwitterButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View paramAnonymousView) { Toast.makeText( MainActivity.this.getApplicationContext(), "waiting to share to twitter", Toast.LENGTH_SHORT).show(); MainActivity.this.shareToTwitter(); } }); ((Button) findViewById(R.id.toWeixinButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View paramAnonymousView) { Toast.makeText( MainActivity.this.getApplicationContext(), "waiting to share to weixin", Toast.LENGTH_SHORT).show(); MainActivity.this.shareToWeixin(); } }); ((Button) findViewById(R.id.toWeiboButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View paramAnonymousView) { Toast.makeText( MainActivity.this.getApplicationContext(), "waiting to share to weibo", Toast.LENGTH_SHORT) .show(); MainActivity.this.shareToWeibo(); } }); } private void shareToFacebook() { shareToByType("facebook"); } private void shareToTwitter() { shareToByType("twitter"); } private void shareToWeibo() { shareToByType("weibo"); } private void shareToWeixin() { Intent localIntent = new Intent(); localIntent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI")); localIntent.setAction("android.intent.action.SEND"); localIntent.setType("image/*"); localIntent.putExtra("android.intent.extra.TEXT", "测试消息"); startActivity(localIntent); } private void shareToByType(String type) { boolean found = false; Intent share = new Intent(android.content.Intent.ACTION_SEND); share.setType("image/jpeg"); List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities( share, 0); if (!resInfo.isEmpty()) { for (ResolveInfo info : resInfo) { if (info.activityInfo.packageName.toLowerCase().contains(type) || info.activityInfo.name.toLowerCase().contains(type)) { share.putExtra(Intent.EXTRA_SUBJECT, "subject"); share.putExtra(Intent.EXTRA_TEXT, "your text"); share.setPackage(info.activityInfo.packageName); found = true; break; } } if (!found) return; startActivity(Intent.createChooser(share, "Select")); } } }