在Android中实现分享有一种比较方便的方式,调用系统的分享面板来分享我们的应用。最基本的实现如下:

	public Intent getShareIntent(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "这是测试分享面板, http://www.baidu.comss");
intent.setType("text/plain");
return intent;
}

还有一种是实现在ActionBar上添加分享列表,实现代码如下:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>

	@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
shareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent shareIntent = getShareIntent();
shareActionProvider.setShareIntent(shareIntent);
return true;
} public Intent getShareIntent(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "这是测试分享面板, http://www.baidu.comss");
intent.setType("text/plain");
return intent;
}

系统默认会为我们找出所有支持seteType中类型的应用,同样我们可以实现自定义分享的平台。

	private void initShareIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(
intent, 0);
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
for (ResolveInfo info : resInfo) {
Intent targeted = new Intent(Intent.ACTION_SEND);
targeted.setType("text/plain");
ActivityInfo activityInfo = info.activityInfo;
//在这里可以添加相应的平台,用 || 连接
if (activityInfo.packageName.contains("com.tencent.mm")) {
targeted.putExtra(Intent.EXTRA_TEXT, "分享内容");
targeted.setPackage(activityInfo.packageName);
targetedShareIntents.add(targeted);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
if (chooserIntent == null) {
return;
}
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
try {
startActivity(chooserIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Can't find sharecomponent to share",
Toast.LENGTH_SHORT).show();
}
}
}

系统的分享面板存在一些缺陷,比如每个手机显示的面板的样式不同,不同手机上显示的分享平台种类和数目不同,会出现一些杂乱的应用。我们可以给上面的方法添加参数,让只能分享到一个平台就可以解决这个问题,这样我们就可以自定义一个分享面板,来添加我们想要的应用,代码如下:

	private void initShareIntent(String type) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
// gets the list of intentsthat can be loaded.
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.putExtra(Intent.EXTRA_STREAM,
// Uri.fromFile(newFile(myPath))); // Optional, just
// // if you wanna
// // share an
// // image.
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(share, "Select"));
}
}

Android自定义系统分享面板的更多相关文章

  1. Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博

    原文:Android 调用系统分享文字.图片.文件,可直达微信.朋友圈.QQ.QQ空间.微博 兼容SDK 18以上的系统,直接调用系统分享功能,分享文本.图片.文件到第三方APP,如:微信.QQ.微博 ...

  2. android - 调用系统分享功能分享图片

    step1: 编写分享代码, 将Uri的生成方式改为由FileProvider提供的临时授权路径,并且在intent中添加flag 注意:在Android7.0之后,调用系统分享,传入URI的时候可能 ...

  3. Android调用系统分享功能总结

    Android分享-调用系统自带的分享功能 实现分享功能的几个办法 1.调用系统的分享功能 2.通过第三方SDK,如ShareSDK,友盟等 3.自行使用各自平台的SDK,比如QQ,微信,微博各自的S ...

  4. Android调用系统分享功能以及createChooser的使用

    工程结构 //效果图 点击测试分享                                                                                   ...

  5. Android 实现系统分享

    使用手机上的程序,来分享/发送,比如QQ的“发送到我的电脑”. 1.分享/发送文本内容 Intent shareIntent = new Intent(); shareIntent.setAction ...

  6. Android定义自己的面板共享系统

    在Android分享知道有一个更方便的方法.调用的共享面板来分享我们的应用程序的系统.主要实现例如,下面的: public Intent getShareIntent(){ Intent intent ...

  7. Android应用中实现系统“分享”接口

    在android下各种文件管理器中,我们选择一个文件,点击分享可以看到弹出一些app供我们选择,这个是android系统分享功能,我们做的app也可以出现在这个列表中. 第一步:在Manifest.x ...

  8. Android调用系统相机、自定义相机、处理大图片

    Android调用系统相机和自定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显示出来,该例子也会涉及到Android加载大图片时候的处理 ...

  9. Android自定义日历控件(继承系统控件实现)

    Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...

随机推荐

  1. HDU 4183Pahom on Water(网络流之最大流)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4183 这题题目意思非常难看懂..我看了好长时间也没看懂..终于是从网上找的翻译. .我就在这翻译一下吧 ...

  2. CKEditor高级编辑器

    是否感觉后台分类描写叙述信息.商品描写叙述信息以及文章描写叙述信息 编写时很的不方便?有时候会将word的格式也复制过来了?那这个插件就比較适合了. 本插件使用CKEditor最新版本号,复制过来的内 ...

  3. 8. java操作mongodb——查询数据

    转自:https://www.cnblogs.com/adjk/p/6430074.html 通过find方法查询集合中的文档信息 ---------------------------------- ...

  4. zip---解压缩文件

    zip命令可以用来解压缩文件,或者对文件进行打包操作.zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有“.zip”扩展名的压缩文件. 语法 zip(选项)(参数) 选项 -A:调整可执行的自 ...

  5. OpenJDK源码研究笔记(十):枚举的高级用法,枚举实现接口,竟是别有洞天

    在研究OpenJDK,Java编译器javac源码的过程中,发现以下代码. 顿时发现枚举类竟然也有如此"高端大气上档次"的用法. 沙场点兵(用法源码) com.sun.tools. ...

  6. HRBUST 1819 石子合并问题--圆形版

    石子合并问题--圆形版 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HRBUST. Original ...

  7. Nagle和Cork

    我觉得这篇讲的不错. http://blog.csdn.net/c_cyoxi/article/details/8673645 Nagle算法的基本定义是任意时刻,最多只能有一个未被确认的小段. 关闭 ...

  8. Starting nagios:This account is currently not available.

    解决方式: 又一次安装php 再重新启动apache 再启动nagios 再訪问:http://ip/nagios 我的问题就是 解决的.

  9. android 4.4最新官方源代码下载

    国内网络,日夜不休花了一个多月才下载成功android标准源代码,有些开发同人须要.已上传到网盘,分享给大家 微云地址: http://url.cn/PkkSzC 百度云盘地址(更新) http:// ...

  10. 蓝的成长记——追逐DBA(10):飞刀防身,熟络而非专长:摆弄中间件Websphere

    原创作品,出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...