文章:
https://www.jianshu.com/p/9522e24713e1
1、定义一个工具类(PlatformUtil)
调用方式如:Platformutil.isInstallApp(context,PlatformUtil.PACKAGE_WECHAT);// 判断微信是否安装
public class PlatformUtil {
public static final String PACKAGE_WECHAT = "com.tencent.mm";
public static final String PACKAGE_MOBILE_QQ = "com.tencent.mobileqq";
public static final String PACKAGE_QZONE = "com.qzone";
public static final String PACKAGE_SINA = "com.sina.weibo";
// 判断是否安装指定app
public static boolean isInstallApp(Context context, String app_package){
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pInfo = packageManager.getInstalledPackages(0);
if (pInfo != null) {
for (int i = 0; i < pInfo.size(); i++) {
String pn = pInfo.get(i).packageName;
if (app_package.equals(pn))) {
return true;
}
}
}
return false;
}
}
2、qq分享
/**
* 直接分享纯文本内容至QQ好友
* @param mContext
* @param content
*/
public static void shareQQ(Context mContext, String content) {
if (PlatformUtil.isInstallApp(mContext, PlatformUtil.PACKAGE_MOBILE_QQ)) {
Intent intent = new Intent("android.intent.action.SEND");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, content);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity"));
mContext.startActivity(intent);
} else {
Toast.makeText(mContext, "您需要安装QQ客户端", Toast.LENGTH_LONG).show();
}
}
调用:
shareQQ(当前上下文,要传的文本)
shareQQ(getContext(),newsList.get(position).getTitle().concat(newsList.get(position).getUrl()));
3、微信分享
/**
* 直接分享文本到微信好友
*
* @param context 上下文
*/
public void shareWechatFriend(Context context, String content) {
if (PlatformUtil.isInstallApp(context, PlatformUtil.PACKAGE_WECHAT)) {
Intent intent = new Intent();
ComponentName cop = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
intent.setComponent(cop);
intent.setAction(Intent.ACTION_SEND);
intent.putExtra("android.intent.extra.TEXT", content);
// intent.putExtra("sms_body", content);
intent.putExtra("Kdescription", !TextUtils.isEmpty(content) ? content : "");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
Toast.makeText(context, "您需要安装微信客户端", Toast.LENGTH_LONG).show();
}
}
调用:
shareWechatFriend(getContext(),newsList.get(position).getTitle().concat(newsList.get(position).getUrl()));