Android调用系统分享

获取Android系统分享列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public List<AppInfoVo> getShareApps(Context context) {
PackageManager packageManager = context.getPackageManager();
List<AppInfoVo> appInfoVos = new ArrayList<AppInfoVo>();
List<ResolveInfo> resolveInfos = new ArrayList<ResolveInfo>();
Intent intent = new Intent(Intent.ACTION_SEND, null);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("*/*");
PackageManager pManager = context.getPackageManager();
resolveInfos = pManager.queryIntentActivities(intent, PackageManager
.COMPONENT_ENABLED_STATE_DEFAULT);
for (int i = 0; i < resolveInfos.size(); i++) {
AppInfoVo appInfoVo = new AppInfoVo();
ResolveInfo resolveInfo = resolveInfos.get(i);
appInfoVo.setAppName(resolveInfo.loadLabel(packageManager).toString());
appInfoVo.setIcon(resolveInfo.loadIcon(packageManager));
appInfoVo.setPackageName(resolveInfo.activityInfo.packageName);
appInfoVo.setLauncherName(resolveInfo.activityInfo.name);
appInfoVos.add(appInfoVo);
}
return appInfoVos;
}

分享文本信息

1
2
3
4
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"This is a text");
startActivity(Intent.createChooser(intent,"Share"));

分享单张图片

1
2
3
4
5
6
7
String path = Environment.getExternalStorageDirectory() + File.separator;//sd根目录
File file = new File(path, "test" + ".png");//这里test.png是sd卡根目录下的一个图片文件
Uri imageUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, "Share"));

分享视频

1
2
3
4
5
6
7
8
9
10
String path = Environment.getExternalStorageDirectory() + File.separator;//sd根目录
File file = new File(path, "test" + ".mp4");
Uri audioUri = Uri.fromFile(file);
ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, audioUri);
shareIntent.setType("audio/*");
startActivity(Intent.createChooser(shareIntent, "分享视频"));

分享多个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
String path = Environment.getExternalStorageDirectory() + File.separator;//sd根目录

ArrayList<Uri> imageUris = new ArrayList<>();
File file = new File(path, "test" + ".png");
File file2 = new File(path, "test2" + ".png");
Uri imageUri = Uri.fromFile(file);
Uri imageUri2 = Uri.fromFile(file2);
imageUris.add(imageUri);
imageUris.add(imageUri2);

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);
startActivity(Intent.createChooser(intent, "Share"));

指定分享到微信朋友

1
2
3
4
5
6
7
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享多张图片"));

指定分享到微信朋友圈

1
2
3
4
5
6
7
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享多张图片"));

指定分享到QQ好友

1
2
3
4
5
6
7
ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享多张图片"));

视频指定分享到QQ空间

1
2
3
4
5
6
7
8
9
10
String path = Environment.getExternalStorageDirectory() + File.separator;//sd根目录
File file = new File(path, "test" + ".mp4");
Uri audioUri = Uri.fromFile(file);
ComponentName comp = new ComponentName("com.qzone", "com.qzonex.module.maxvideo.activity.QzonePublishVideoActivity");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, audioUri);
shareIntent.setType("video/*");
startActivity(Intent.createChooser(shareIntent, "分享视频"));

分享到指定程序

只要获得包名和相应Activity即可

微信朋友圈

“com.tencent.mm”
“com.tencent.mm.ui.tools.ShareToTimeLineUI”

微信朋友

“com.tencent.mm”
“com.tencent.mm.ui.tools.ShareImgUI”

QQ好友

“com.tencent.mobileqq”
“com.tencent.mobileqq.activity.JumpActivity”

QQ空间分享视频

“com.qzone”
“com.qzonex.module.maxvideo.activity.QzonePublishVideoActivity”

QQ空间分享图片、文字

“com.qzone”
“com.qzonex.module.operation.ui.QZonePublishMoodActivity”

新浪微博

“com.sina.weibo”
“com.sina.weibo.composerinde.ComposerDispatchActivity”

坚持原创技术分享,您的支持将鼓励我继续创作!