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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM