public class ShareUtil { /** * 判斷是否安裝騰訊、新浪等指定的分享應用 * * @param packageName 應用的包名 */ public static boolean checkInstallation(Activity activity, String packageName) { try { activity.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } /** * 分享到QQ好友 */ private void shareToQQFriend(Activity activity) { Intent intent = new Intent(); ComponentName componentName = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity"); intent.setComponent(componentName); intent.setAction(Intent.ACTION_SEND); intent.setType("text/*"); intent.putExtra(Intent.EXTRA_TEXT, "這是分享內容"); activity.startActivity(intent); } /** * 分享信息到朋友 * */ private void shareToWxFriend(Activity activity) { Intent intent = new Intent(); ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI"); intent.setComponent(componentName); intent.setAction(Intent.ACTION_SEND); intent.setType("text/*"); intent.putExtra(Intent.EXTRA_TEXT, "這是分享內容"); intent.putExtra(Intent.EXTRA_STREAM, "http://www.weixin.com"); activity.startActivity(intent); } /** * 分享信息到朋友圈 * */ private void shareToTimeLine(Activity activity) { Intent intent = new Intent(); ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"); intent.setComponent(componentName); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM,"http://www.weixin.com"); intent.setType("p_w_picpath/*"); activity.startActivity(intent); } public void qqshare(Activity activity) { //Tencent類是SDK的主要實現類,開發者可經過Tencent類訪問騰訊開放的OpenAPI。 // 其中APP_ID是分配給第三方應用的appid,類型爲String。 Tencent mTencent = Tencent.createInstance("1111112", x.app().getApplicationContext()); ShareListener myListener = new ShareListener(); final Bundle params = new Bundle(); params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT); params.putString(QQShare.SHARE_TO_QQ_TITLE, "要分享的標題"); params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "要分享的摘要"); params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://www.qq.com/news/1.html"); params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "https://www.baidu.com/img/bd_logo1.png"); mTencent.shareToQQ(activity, params, myListener); } private class ShareListener implements IUiListener { @Override public void onCancel() { // TODO Auto-generated method stub Toast.makeText(x.app(), "分享取消", Toast.LENGTH_SHORT).show(); // MainActivity.this.toast("分享取消"); } @Override public void onComplete(Object arg0) { // TODO Auto-generated method stub Toast.makeText(x.app(), "分享成功", Toast.LENGTH_SHORT).show(); } @Override public void onError(UiError arg0) { // TODO Auto-generated method stub Toast.makeText(x.app(), "分享出錯", Toast.LENGTH_SHORT).show(); } } }