Android Intent.ACTION_SEND ——实现分享功能

安卓系统本身可以很简便的实现分享功能,因为我们只需向startActivity传递一个ACTION_SEND的Intent,系统就为我们弹出一个应用程序列表。其实在系统的文件管理器中,这应该是我们常用的功能(包括文件的打开Intent.ACTION_VIEW)。

下面列出一个简单的分享方式

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

前两行代码不用说了,就是一个简单的Action Intent,第三行的Intent.EXTRA_TEXT,是文本类型,还有EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT等等,这些看字面意思就可以理解。 
重点说一下Intent.EXTRA_STREAM。 
设置合适的MIME类型,并且在附件数据中的EXTRA_STREAM中放一个指向数据的URI,就可以来分享二进制数据。这个通常用来分享图片,但是也可以用来分享任何类型的二进制内容,比如视频,文件等等。

Intent shareIntent = newIntent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

下面说一下Intent.setType这个方法: 
参数有很多种,简单列出几个,”text/plain”、”image/jpeg”、”audio/mp4a-latm”、”audio/x-mpeg”、 “video/mp4”还有很多很多… 
这里给出一个获取类型的方法

    /**
     * 根据文件后缀名获得对应的MIME类型。
     * @param filePath
     */
    public static String getMimeType(String filePath) {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        String mime = "text/plain";
        if (filePath != null) {
            try {
                mmr.setDataSource(filePath);
                mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
            } catch (IllegalStateException e) {
                return mime;
            } catch (IllegalArgumentException e) {
                return mime;
            } catch (RuntimeException e) {
                return mime;
            }
        }
        return mime;
    }
我们可以**同时发送多条内容**,要发送多条数据,使用ACTION_SNED_MULTIPLE和一个指向数据的URI list。MIME类型根据分享的内容不同而不同。例如,如果分享3张JPEG图片,那么类型为"image/jpeg"。如果有不同的图片类型,那么就应该用"image/*"来匹配处理不同类型图片的activity。如果要处理各种不同的类型就应该用"*/*"了。正如前面提到的,分析和处理分享是数据是接收程序的事情了。
但是需要明确的一点是,**要确保URI指向的数据要可以被接收程序访问到**。

另外一个知识点就是,我们可以对分享的App进行筛选,比如我只想分享到QQ和微信平台,不关心人人网,迅雷这样的App
可通过Intent.createChooser方法实现,
首先我们定义一个Action Intent
String type = getMimeType(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareIntent.setType(getMimeType(path));

然后获取可以用来发送该类型文件的ResolveInfo列表,也就是可以发送这种文件的应用列表信息

List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(shareIntent, 0);

通过包名筛选出我们想要的应用

ArrayList<Intent> targetIntents = new ArrayList<Intent>();
                for (ResolveInfo info : resInfo) {
                    ActivityInfo activityInfo = info.activityInfo;
                    if (activityInfo.packageName.contains("com.tencent.mobileqq")
                            ||activityInfo.packageName.contains("com.tencent.mm")) {
                        Intent intent = new Intent(Intent.ACTION_SEND);
                        intent.setPackage(activityInfo.packageName);
                        intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
                        intent.setClassName(activityInfo.packageName, activityInfo.name);
                        targetIntents.add(intent);
                    }


                }

最后用Intent.createChooser打开

Intent chooser = Intent.createChooser(targetIntents.remove(0), "Send mail...");
                chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
                context.startActivity(chooser);

到此,利用Intent.ACTION_SEND进行分享就差不多介绍完了,是不是比申请友盟以及各个平台要方便的多…

附上源码,以作备用

   /**
     * 发送文件
     * @param context
     * @param path
     */
    public static void sendFileByOtherApp(Context context, String path) {
        File file = new File(path);
        if (file.exists()) {
            String type = getMimeType(path);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareIntent.setType(getMimeType(path));
            List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(shareIntent, 0);
            if (!resInfo.isEmpty()) {
                ArrayList<Intent> targetIntents = new ArrayList<Intent>();
                for (ResolveInfo info : resInfo) {
                    ActivityInfo activityInfo = info.activityInfo;
                    if (activityInfo.packageName.contains("com.tencent.mobileqq")
                            ||activityInfo.packageName.contains("com.tencent.mm")) {
                        Intent intent = new Intent(Intent.ACTION_SEND);
                        intent.setPackage(activityInfo.packageName);
                        intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
                        intent.setClassName(activityInfo.packageName, activityInfo.name);
                        targetIntents.add(intent);
                    }


                }
                Intent chooser = Intent.createChooser(targetIntents.remove(0), "Send mail...");
                chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
                context.startActivity(chooser);

            }
        }

    }

当然,我们也可以做一个Activity,像QQ微信一样,来接收文件或者wen z,只要会使用intentfilter就可以了

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

然后在oncreate方法中

if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // 处理发送来的文字
        } else if (type.startsWith("image/")) {
            handleSendImage(intent); // 处理发送来的图片
        }
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            handleSendMultipleImages(intent); // 处理发送来的多张图片
        }
    } else {
        // 处理其他intents,比如由主屏启动
    }

这样基本用法就介绍完了,基本的分享功能差不多可以完成,但是想要分享图文内容,或者自定义分享界面,可能就需要再深度挖掘了。

转载url : https://blog.csdn.net/wuzhiguo1314/article/details/52950764

猜你喜欢

转载自blog.csdn.net/adminlxb89/article/details/81457480