Android 接收微信、QQ其他应用打开,第三方分享
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

在AndroidManifest.xml注册ACTION事件
<activity
android:name="com.test.app.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="这里的名称会对外显示"
android:launchMode="singleTask"
android:screenOrientation="portrait">
//注册接收分享
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" /> //接收分享的文件类型
<data android:mimeType="image/*" />
<data android:mimeType="application/msword" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
<data android:mimeType="application/vnd.ms-excel" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
<data android:mimeType="application/vnd.ms-powerpoint" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
<data android:mimeType="application/pdf" />
<data android:mimeType="text/plain" />
</intent-filter> //注册默认打开事件,微信、QQ的其他应用打开
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" /> //接收打开的文件类型
<data android:scheme="file" />
<data android:scheme="content" />
<data android:mimeType="image/*" />
<data android:mimeType="application/msword" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
<data android:mimeType="application/vnd.ms-excel" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
<data android:mimeType="application/vnd.ms-powerpoint" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
<data android:mimeType="application/pdf" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
在用于接收分享的Activity里面加接收代码
- 当APP进程在后台时,会调用Activity的onNewIntent方法
- 当APP进程被杀死时,会调用onCreate方法
所以在两个方法中都需要监听事件
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
receiveActionSend(intent);
} @Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
receiveActionSend(intent);
}
receiveActionSend方法如下
public void receiveActionSend(Intent intent) {
String action = intent.getAction();
String type = intent.getType();
//判断action事件
if (type == null || (!Intent.ACTION_VIEW.equals(action) && !Intent.ACTION_SEND.equals(action))) {
return;
}
//取出文件uri
Uri uri = intent.getData();
if (uri == null) {
uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
}
//获取文件真实地址
String filePath = UriUtils.getFileFromUri(EdusohoApp.baseApp, uri);
if (TextUtils.isEmpty(filePath)) {
return;
}
//业务处理
.
.
.
}
获取真实路径getFileFromUri方法
/**
* 获取真实路径
*
* @param context
*/
public static String getFileFromUri(Context context, Uri uri) {
if (uri == null) {
return null;
}
switch (uri.getScheme()) {
case ContentResolver.SCHEME_CONTENT:
//Android7.0之后的uri content:// URI
return getFilePathFromContentUri(context, uri);
case ContentResolver.SCHEME_FILE:
default:
//Android7.0之前的uri file://
return new File(uri.getPath()).getAbsolutePath();
}
}
Android7.0之后的uri content:// URI需要对微信、QQ等第三方APP做兼容
- 在文件管理选择本应用打开时,url的值为content://media/external/file/85139
- 在微信中选择本应用打开时,url的值为 content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc
- 在QQ中选择本应用打开时,url的值为 content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/
第一种为系统统一文件资源,能通过系统方法转化为绝对路径;
微信、QQ的为fileProvider,只能获取到文件流,需要先将文件copy到自己的私有目录。
方法如下:
/**
* 从uri获取path
*
* @param uri content://media/external/file/109009
* <p>
* FileProvider适配
* content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/
* content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/
*/
private static String getFilePathFromContentUri(Context context, Uri uri) {
if (null == uri) return null;
String data = null; String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
int index = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
if (index > -1) {
data = cursor.getString(index);
} else {
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
String fileName = cursor.getString(nameIndex);
data = getPathFromInputStreamUri(context, uri, fileName);
}
}
cursor.close();
}
return data;
} /**
* 用流拷贝文件一份到自己APP私有目录下
*
* @param context
* @param uri
* @param fileName
*/
private static String getPathFromInputStreamUri(Context context, Uri uri, String fileName) {
InputStream inputStream = null;
String filePath = null; if (uri.getAuthority() != null) {
try {
inputStream = context.getContentResolver().openInputStream(uri);
File file = createTemporalFileFrom(context, inputStream, fileName);
filePath = file.getPath(); } catch (Exception e) {
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
}
}
} return filePath;
} private static File createTemporalFileFrom(Context context, InputStream inputStream, String fileName)
throws IOException {
File targetFile = null; if (inputStream != null) {
int read;
byte[] buffer = new byte[8 * 1024];
//自己定义拷贝文件路径
targetFile = new File(context.getExternalCacheDir(), fileName);
if (targetFile.exists()) {
targetFile.delete();
}
OutputStream outputStream = new FileOutputStream(targetFile); while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
outputStream.flush(); try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} return targetFile;
}
如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

Android 接收微信、QQ其他应用打开,第三方分享的更多相关文章
- Android 实现微信QQ分享以及第三方登录
集成准备 在微信开放平台创建移动应用,输入应用的信息,包括移动应用名称,移动应用简介,移动应用图片信息,点击下一步,选择Android 应用,填写信息提交审核. 获取Appkey 集成[友盟+]SDK ...
- Android仿微信QQ等实现锁屏消息提醒
demo代码如下: import android.content.Intent; import android.os.Bundle; import android.support.v7.app.App ...
- Android APP代码拨打电话、打开手机分享功能等隐式意图
Android APP拨打电话: Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+110)); start ...
- 微信QQ打开网页时提示用浏览器打开
微信QQ打开网页时提示用浏览器打开 一,需求分析 1.1,使用微信或QQ打开网址时,无法在微信或QQ内打开常用下载软件,手机APP等.故此需要在微信qq里提示 二,功能实现 2.1 html实现 &l ...
- android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
开发中常常有打开本地浏览器加载url或者指定浏览器加载, 还有打开第三方app, 如 打开高德地图 百度地图等 在Android程序中我们可以通过发送隐式Intent来启动系统默认的浏览器. 如果手机 ...
- Android通过微信实现第三方登录并使用OKHttp获得Token及源码下载
这里对于App在微信开放平台上申请AppID和secret在这里就略过了,我们微信的授权登录流程,腾讯官网给的流程如下: 1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用 ...
- ios第三方分享到qq、微信、人人网、微博总结
我们开发出来的APP通常要通过第三方分享到其他社交平台,如qq.微博微信 等.通过分享可以提高APP的传播效率,增加APP的曝光率,因此也算是APP功能 里的标配了吧.目前常用的第三方分享途径有qq. ...
- android 程序打开第三方程序
因为在开发过程中需要开启扫描第三方程序,并且点击启动的效果,所以对这个功能进行了实现,并且分享出来个大家. 之前看到网上说需要获取包名和类名,然后通过 intent 才能打开这个程序,其实不必要这样 ...
- 使用ShareSDK完成第三方(QQ、微信、微博)登录和分享
这几天遇到一个需求:做第三方登录和分享.遇到了一些坑,把整个过程整理记录下来,方便他人,同时也捋一下思路. 当时考虑过把每个平台的SDK下载下来,一个一个弄,一番取舍后决定还是用ShareSDK.这里 ...
- Android调用微信登陆、分享、支付
前言:用了微信sdk各种痛苦,感觉比qq sdk调用麻烦多了,回调过于麻烦,还必须要在指定包名下的actvity进行回调,所以我在这里写一篇博客,有这个需求的朋友可以借鉴一下,以后自己别的项目有用到也 ...
随机推荐
- Power BI 5 DAY
目录 Power BI 数据建模与数据汇总分析 多维数据模型 搭建多维数据模型 搭建方法 注意事项 搭建数据模型思考 数据变量类型 主键ID特点(非空不重复) 星型结构 交叉筛选器方向 单一/两个 连 ...
- NEMU PA 3-3 实验报告
一.实验目的 在上一章PA3-2中,我们实现了分段机制,将48位的虚拟地址vaddr转换成了laddr.为什么不是paddr呢?这就要说到这一章要完成的东西:**分页机制 **. 从80386开始,计 ...
- NC20545 [HEOI2012]采花
题目链接 题目 题目描述 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了 \(n\) 朵花,花有 \(c\) 种颜色(用整 ...
- 将CH340G的USB2TTL扩展出RTS, DTR口
关于 要测试串口中的RTS和DTR, 最常见的USB2TTL基于CH340G, 并没有引出RTS, 然而这个IC是支持这些信号的, 只是PCB上将这些pin留空了. 这块板子的PCB 电路 详细信息可 ...
- Centos7安装php7.4
添加 EPEL and REMI 仓库 yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarc ...
- Java集合框架学习(十二) Arrays类详解
声明:部分内容参考自:https://liuyanzhao.com/2863.html Arrays类介绍 这个类包含了用于操作数组的各种方法,诸如:排序和搜索. 这个类还包含一个静态方法asList ...
- Golang从入门到跑路-从基础到微服务学习路线图
收录的awesome-go项目,学习基础系列,go项目实战,go源码分析,go开发者成长路线图等等,把他们收集起来学习. 地址:https://github.com/jiujuan/go-collec ...
- less变量书写及样式混入
定义变量 定义混入样式 变量及混入样式使用 样式文件中 ~@代表src
- 国内无法下载k8s镜像的解决办法
关于通过各种方法下载k8s相关镜像的方法总结如下: 1.使用Azure中国镜像站,目前限制只能"Azure China IP"可用,不再对外提供服务,此路不通. 2.直接使用mir ...
- Python嵌套绘图并为条形图添加自定义标注
论文绘图时经常需要多图嵌套,正好最近绘图用到了,记录一下使用Python实现多图嵌套的过程. 首先,实现 Seaborn 分别绘制折线图和柱状图. '''绘制折线图''' import seaborn ...