Android 在分享列表添加自己的应用

本文讲述了如何在AndroidManifest中配置IntentFilter,以使自己的应用出现在文件分享列表中,重点在于正确指定mimeType以兼容不同类型的文件,包括文本、Office文档、图片等,并揭示了`*/*`匹配的局限性及特定机型的兼容问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于在分享列表添加应用

How to make my Android app appear in the share list of another specific app

android系统分享功能,将自己的APK加入可分分享的应用列表

目前业务需求,需要在Android系统下,用户从文件管理器长按文件分享的时候,能出现我们的App列表。

参考了上面两篇文章的操作,尝试在Manifest里面直接添加一个

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" />
</intent-filter>

尝试后在分享列表里面还是无我们App应用,后面将单独的mimeType改为text/plain,之后发现在分享文本文件的时候生效了,测试设备为华为的Mate30。

尝试将上面的代码修改为

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="file" />
    <data android:scheme="content" />
    <data android:mimeType="*/*" />
</intent-filter>

想直接通过mimeType匹配全部类型的文件,但是发现该配置添加后,无论是打开纯文本文件分享,或者mp3、图片等二进制数据分享都无效,表现的效果为在分享面板无我们的App展示。

之后结合Stack OverFlow(文章一最后的一条Respon)和文章二里面的mimeType,将IntentFilter依次添加不同的mimeType,分享列表随即能展示出我们的应用了;测试设备依旧为华为Mate30,Harmony 2.0系统。

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:mimeType="text/plain" />
    <data android:mimeType="image/*" />
    <data android:mimeType="application/vnd.ms-powerpoint" />
    <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
    <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/pdf" />
    <data android:mimeType="application/zip" />
    <data android:host="*" />
    <data android:pathPattern=".*" />
</intent-filter>

总结:

  • Manifest需要添加IntentFilter配置,ACTION需要设置为Intent.SEND,category为default
  • 依次将不同的mimeType添加到IntentFilter上,可能因为兼容问题,不能直接用*/*匹配
  • data里面需要注意不能添加scheme = file或者content,否则无法适配(应该是部分机型的问题)
  • 使用IntentFilter的activity,必须要在里面加上exported = true
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值