这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

在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里面加接收代码

  1. 当APP进程在后台时,会调用Activity的onNewIntent方法
  2. 当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其他应用打开,第三方分享的更多相关文章

  1. Android 实现微信QQ分享以及第三方登录

    集成准备 在微信开放平台创建移动应用,输入应用的信息,包括移动应用名称,移动应用简介,移动应用图片信息,点击下一步,选择Android 应用,填写信息提交审核. 获取Appkey 集成[友盟+]SDK ...

  2. Android仿微信QQ等实现锁屏消息提醒

    demo代码如下: import android.content.Intent; import android.os.Bundle; import android.support.v7.app.App ...

  3. Android APP代码拨打电话、打开手机分享功能等隐式意图

    Android APP拨打电话: Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+110)); start ...

  4. 微信QQ打开网页时提示用浏览器打开

    微信QQ打开网页时提示用浏览器打开 一,需求分析 1.1,使用微信或QQ打开网址时,无法在微信或QQ内打开常用下载软件,手机APP等.故此需要在微信qq里提示 二,功能实现 2.1 html实现 &l ...

  5. android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app

    开发中常常有打开本地浏览器加载url或者指定浏览器加载, 还有打开第三方app, 如 打开高德地图 百度地图等 在Android程序中我们可以通过发送隐式Intent来启动系统默认的浏览器. 如果手机 ...

  6. Android通过微信实现第三方登录并使用OKHttp获得Token及源码下载

    这里对于App在微信开放平台上申请AppID和secret在这里就略过了,我们微信的授权登录流程,腾讯官网给的流程如下: 1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用 ...

  7. ios第三方分享到qq、微信、人人网、微博总结

    我们开发出来的APP通常要通过第三方分享到其他社交平台,如qq.微博微信 等.通过分享可以提高APP的传播效率,增加APP的曝光率,因此也算是APP功能 里的标配了吧.目前常用的第三方分享途径有qq. ...

  8. android 程序打开第三方程序

    因为在开发过程中需要开启扫描第三方程序,并且点击启动的效果,所以对这个功能进行了实现,并且分享出来个大家. 之前看到网上说需要获取包名和类名,然后通过  intent 才能打开这个程序,其实不必要这样 ...

  9. 使用ShareSDK完成第三方(QQ、微信、微博)登录和分享

    这几天遇到一个需求:做第三方登录和分享.遇到了一些坑,把整个过程整理记录下来,方便他人,同时也捋一下思路. 当时考虑过把每个平台的SDK下载下来,一个一个弄,一番取舍后决定还是用ShareSDK.这里 ...

  10. Android调用微信登陆、分享、支付

    前言:用了微信sdk各种痛苦,感觉比qq sdk调用麻烦多了,回调过于麻烦,还必须要在指定包名下的actvity进行回调,所以我在这里写一篇博客,有这个需求的朋友可以借鉴一下,以后自己别的项目有用到也 ...

随机推荐

  1. 如何使用ASP.NET Core 中的 Hangfire 实现作业调度

    https://procodeguide.com/programming/hangfire-in-aspnet-core-schedule-jobs/ 如何使用ASP.NET Core 中的 Hang ...

  2. JS实现一个布隆过滤器

    之前专门聊过令牌桶算法,而类似的方案还有布隆过滤器.它一般用于高效地查找一个元素是否在一个集合中. 用js实现如下所示: class BloomFilter { constructor(size, h ...

  3. 同一份代码怎能在不同环境表现不同?记一个可选链因为代码压缩造成的bug

    壹 ❀ 引 某一天,CSM日常找我反馈客户紧急工单,说有一个私有部署客户升级版本后,发现一个功能使用不太正常.因为我们公司客户分为两种,一种是SaaS客户,客户侧使用的版本被动跟随主版本变动,而私有部 ...

  4. gif 制作

    gif 制作 博文中使用 gif 有时比纯粹的图片更明了.比如展示"墨刀"中的动画效果: 录制视频 首先利用录制视频,例如使用在线录制工具 vizard. Tip:需要花费2分钟手 ...

  5. USB至串口TTL转接设备及Console线

    USB转串口常见芯片方案 FT232, FTDI(英国) 公认稳定可靠, 传输速率3Mbps, 功能最强, 单芯片内置SPI,TWI,JTAG,GPIO等功能. FT232BM为较早型号, FT232 ...

  6. Spring Boot图书管理系统项目实战-6.图书管理

    导航: pre:  5.读者管理 next:7.借阅图书 只挑重点的讲,具体的请看项目源码. 1.项目源码 需要源码的朋友,请捐赠任意金额后留下邮箱发送:) 2.页面设计 2.1 book.html ...

  7. MinGW 和 MSVC

    在 Winodws 上编译通常会用到这两种工具链 MinGW(Minimalist GNU for Windows) 通常用于跨平台开发,可以编译出在 Windows 系统上运行的 .exe 程序 M ...

  8. 项目实战:Qt+OSG三维2D文字实时效果查看工具

    需求   OSG三维中2D文字的基本属性较多,方便实时查看效果,并出对应文本代码.   Demo      工具下载地址   CSDN免积分下载地址:https://download.csdn.net ...

  9. pgrep查询当前运行程序的pid

    pgrep 运行的程序 [root@c1 ~]# pgrep matmul 2634730

  10. GPS坐标系转换 go golang 版本

    GPS坐标系转换 坐标系 解释 WGS84坐标系 地球坐标系,国际通用坐标系 GCJ02坐标系 火星坐标系,WGS84坐标系加密后的坐标系:Google国内地图.高德.腾讯地图 使用 BD09坐标系 ...