Android 分享功能实现与 QQ 纯文本分享

来源:互联网 发布:apache tomcat6.0下载 编辑:程序博客网 时间:2024/06/01 08:29

Android 分享功能实现与 QQ 纯文本分享

前言

题外话:有没有碰到过,分享带 二维码的图片 给微信 好友的时候,Andriod 端不能弹出 二维码识别的选项? 如果有,请点击 Android
分享功能之 微信 图片分享

在 Android 系统中如何给应用增加分享功能?怎样将应用加入系统的分享选择列表?

通过系统分享组件实现分享功能

Intent.createChooser() 方法用来弹出系统分享列表。
createChooser(Intent target, CharSequence title, IntentSender sender) 参数。

常规方法

 public void share(Context context){        Intent sendIntent = new Intent();        sendIntent.setAction(Intent.ACTION_SEND);        sendIntent.putExtra(Intent.EXTRA_TEXT, content);        sendIntent.setType("text/plain");        context.startActivity(sendIntent);  }

可以调用 手机中 所有的开发分享接口的应用,进行分享。

选取特定应用分享方法

举例 :纯文本分享给 QQ 好友(QQ 官方分享 SDK 是不支持纯文本分享的,但通过这种方式可以)

 public void shareQQ(Context mContext){    Intent sendIntent = new Intent();        sendIntent.setAction(Intent.ACTION_SEND);        sendIntent.putExtra(Intent.EXTRA_TEXT, content);        sendIntent.setType("text/plain");        //sendIntent.setPackage("com.tencent.mobileqq");        // List<ResolveInfo> list= getShareTargets(mContext);        try {            sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");            if (chooserIntent == null) {                return;            }            mContext.startActivity(chooserIntent);        } catch (Exception e) {            mContext.startActivity(sendIntent);        }    }

获得 手机 中 支持 纯文本分享的所有列表:

   /* 获得支持ACTION_SEND的应用列表 */    private List<ResolveInfo> getShareTargets(Context context) {        Intent intent = new Intent(Intent.ACTION_SEND, null);        intent.addCategory(Intent.CATEGORY_DEFAULT);        intent.setType("text/plain");        PackageManager pm = context.getPackageManager();        return pm.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);    }

其中涉及的两个 对象,可以点击查看详细说明:
ResolveInfo
ActivityInfo


怎样将自己的应用加入系统分享组件中?

先看一个腾讯微博的例子(网友反编译后的例子)

<activity android:name=".activity.MicroBlogInput" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="stateAlwaysVisible|adjustResize">   <intent-filter android:label="@string/albums_sendbyWBlog">                       <action android:name="android.intent.action.SEND" />                       <data android:mimeType="image/*" />                                <category android:name="android.intent.category.DEFAULT" />              </intent-filter></activity> 

通过上面的可以看出下面这些是关键:

<intent-filter android:label="@string/albums_sendbyWBlog">                  <action android:name="android.intent.action.SEND" />                   <data android:mimeType="image/*" />                            <category android:name="android.intent.category.DEFAULT" />            </intent-filter>

其中支持的类型可以多种,包括图片、纯文本、二进制等;

 <data android:mimeType="image/*" />   //可以是text/plain

官方文档:mimeType

通过以上即可实现分享和添加应用到分享列表的功能了。

2 0
原创粉丝点击