android - 最佳实践 - 将您的应用添加到 Android 共享菜单

标签 android android-intent

我的应用通过 TCP 连接发送文件(所有 mime 类型),因此我希望我的应用出现在 Android 共享菜单中。

我在 AndroidManifest.xml 中将以下 Intent 过滤器添加到我的 Activity

<intent-filter ><!--intent filter for single file sharing-->
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" />
</intent-filter>
<intent-filter ><!--intent filter for sharing multiple files-->
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" />
</intent-filter>

现在我将它添加到 Activity 中,该 Activity 将在单击共享操作时启动

Intent intent = getIntent();
if (Intent.ACTION_SEND.equals(intent.getAction())) {
    Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (uri != null) {
        fileset = new HashSet();
        fileset.add(getPathfromUri(uri));
    }
} else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
    ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    if (uris != null) {
        fileset = new HashSet();
        for(Uri uri : uris) {
            fileset.add(getPathfromUri(uri));
        }
    }
}

我使用这种方法来生成要共享的文件的绝对路径。

public String getPathfromUri(Uri uri) {
    if(uri.toString().startsWith("file://")) {
         return uri.getPath();
    }
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String path= cursor.getString(column_index);
    //cursor.close();
    return path;
}

上述方法正确检索图像/视频和其他文件的绝对路径。是否有我遗漏了什么或者有更好的方法来做到这一点?

除了 Share 菜单,我的手机上还有一个 Send Via 菜单。 screenshot

有没有办法让我的应用进入这个列表?

最佳答案

我已经在这里概述了如何进入菜单:

http://blog.blundellapps.com/add-your-app-to-the-android-menu/

基本上它有你的 mimetype / :

        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="*/*" />
        </intent-filter>

如果你这样做但它没有显示,这意味着他们正在使用一个 Intent 菜单,它是一个自定义共享列表,所以你不能强行进入。

关于android - 最佳实践 - 将您的应用添加到 Android 共享菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788252/

相关文章:

android - 在 Android 上的 GPS 定位方面需要帮助

java - 如何设置 ImageView 的角半径

java - 如何在 fragment 的两个不同类中发送 Intent

Android视频播放器开始使用 Intent ,如何知道播放器何时停止

java - 在 Android 应用程序之间共享数据的最佳实践

mac 10.5.8 上的 android 错误 dyld 未知需要加载命令 0x00000022

android - 应用更新未显示在Google Play商店中

android - Xamarin Forms 项目模拟器和设备上的空白谷歌地图

java - Intent 在 Side-Menu.Android 的 Material 设计中 Activity

android - 接收广播时出错 Intent act=android.net.wifi.p2p.THIS_DEVICE_CHANGED