现在大多数App的分享功能基本上使用的都是微信、QQ、微博的分享sdk或者是集成友盟分享sdk,如果想简单快速的实现分享功能,可以考虑通过Intent
方式打开在系统中安装的具有分享功能的App的分享页面。方法如下:
public static List<ResolveInfo> shareIntentActivities(Context context){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intnet.setType("text/plain");
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
return resloveInfos;
}
如果只需要QQ和微信分享,可以进行如下操作:
public static List<ResolveInfo> shareIntentActivities(Context context){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intnet.setType("text/plain");
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
List<ResolveInfo> shareItems = new ArrayList();
for(ResolveInfo info: resolveInfos) {
String name = info.activityInfo.packageName;
if (TextUtils.equals(packageName, "com.tencent.mm")
|| TextUtils.equals(packageName, "com.tencent.mobileqq")) {
shareItems.add(resolveInfo);
}
}
return shareItems;
}
通过ResolveInfo
获取图标ICON
和Label
:
Drawable drawable = resloveInfo.loadIcon(packageManager);
CharSequence label = resolveInfo.loadLabel(packageManager);
通过Intent
发起分享:
public static void share(Context context,
ResolveInfo info, String shareContent){
Intent itnent = new Intent();
intnet.setAction(Intent.ACTION_SEND);
String pkg = resolveInfo.activityInfo.packageName;
String cls = resolveInfo.activityInfo.name;
intent.setComponent(new Component(pkg, cls));
intent.putExtra(Intent.EXTRA_TEXT, shareContent);
context.startActivity(intent);
}