能用代码说话,咱就用代码说话。
class SharedUtils private constructor() {
companion
object {
val i: SharedUtils
by lazy { SharedUtils() }
/** * 微信朋友圈 */
val weiXinComponent: ComponentName
by lazy { ComponentName(
"com.tencent.mm",
"com.tencent.mm.ui.tools.ShareToTimeLineUI" ) } }
/** * 分享到手机中所有的平台 * @param context * @param conTentType 内容的类型 * @param title 标题 * @param text 内容文本 * @param uri 单个内容的uri * @param uris 多个内容的uri */
private
fun share( context: Context, conTentType: String, title: String, text: String? = null, uri: Uri? = null, uris: ArrayList<Uri>? = null, ) {
val intent = Intent.createChooser(Intent().apply { action = Intent.ACTION_SEND
if (conTentType ==
"text/plain") { putExtra(Intent.EXTRA_TEXT, text) }
else { addCategory(
"android.intent.category.DEFAULT") addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) } uri?.let { putExtra(Intent.EXTRA_STREAM, uri) } uris?.let { putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris) } type = conTentType }, title) context.startActivity(intent) }
/** * 分享到某个平台 * @param context * @param conTentType 内容的类型 * @param text 内容文本 * @param uri 单个内容的uri * @param uris 多个内容的uri * @param packageName 包名,分享到某个平台 * @param component 可以分享到某个平台的某个位置 */
private
fun shareToPlatform( context: Context, conTentType: String, text: String? = null, uri: Uri? = null, uris: ArrayList<Uri>? = null, packageName: String? = null, component: ComponentName? = null ) { context.startActivity(Intent().apply { action = Intent.ACTION_SEND
if (conTentType ==
"text/plain") { putExtra(Intent.EXTRA_TEXT, text) }
else { addCategory(
"android.intent.category.DEFAULT") addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) } uri?.let { putExtra(Intent.EXTRA_STREAM, uri) } uris?.let { putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris) } type = conTentType packageName?.let { `
package` = packageName } component?.let { setComponent(component) } }) }
/** * 分享文字 * @param context * @param title 标题 * @param text 分享内容 * @param packageName 指定平台 * @param component 指定平台下的某个位置 */
fun shareText( context: Context, title: String = "分享", text: String, packageName: String? = null, component: ComponentName? = null ) {
if (packageName ==
null && component ==
null) { share(context = context, conTentType =
"text/plain", title = title, text = text) }
else { shareToPlatform( context = context, conTentType =
"text/plain", text = text, packageName = packageName, component = component ) } }
/** * 分享图片 * @param context * @param title 标题 * @param uri 分享的图片 * @param packageName 指定平台 * @param component 指定平台下的某个位置 */
fun shareImage( context: Context, title: String = "分享", uri: Uri, packageName: String? = null, component: ComponentName? = null ) {
if (packageName ==
null && component ==
null) { share(context = context, conTentType =
"image/*", title = title, uri = uri) }
else { shareToPlatform( context = context, conTentType =
"image/*", uri = uri, packageName = packageName, component = component ) } }
/** * 分享多张图片 * @param context * @param title 标题 * @param imageUris 分享的图片 * @param packageName 指定平台 * @param component 指定平台下的某个位置 */
fun shareImages( context: Context, title: String = "分享", imageUris: ArrayList<Uri>, packageName: String? = null, component: ComponentName? = null ) {
if (packageName ==
null && component ==
null) { share(context = context, conTentType =
"image/*", title = title, uris = imageUris) }
else { shareToPlatform( context = context, conTentType =
"image/*", uris = imageUris, packageName = packageName, component = component ) } }
/** * 分享音频 * @param context * @param title 标题 * @param uri 分享的图片 * @param packageName 指定平台 * @param component 指定平台下的某个位置 */
fun shareAudio( context: Context, title: String = "分享", uri: Uri, packageName: String? = null, component: ComponentName? = null ) {
if (packageName ==
null && component ==
null) { share(context = context, conTentType =
"audio/*", title = title, uri = uri) }
else { shareToPlatform( context = context, conTentType =
"audio/*", uri = uri, packageName = packageName, component = component ) } }
/** * 分享视频 * @param context * @param title 标题 * @param uri 分享的图片 * @param packageName 指定平台 * @param component 指定平台下的某个位置 */
fun shareVideo( context: Context, title: String = "分享", uri: Uri, packageName: String? = null, component: ComponentName? = null ) {
if (packageName ==
null && component ==
null) { share(context = context, conTentType =
"video/*", title = title, uri = uri) }
else { shareToPlatform( context = context, conTentType =
"video/*", uri = uri, packageName = packageName, component = component ) } }
/** * 分享文件 * @param context * @param title 标题 * @param uri 分享的图片 * @param packageName 指定平台 * @param component 指定平台下的某个位置 */
fun shareFile( context: Context, title: String = "分享", uri: Uri, packageName: String? = null, component: ComponentName? = null ) {
if (packageName ==
null && component ==
null) { share(context = context, conTentType =
"*/*", title = title, uri = uri) }
else { shareToPlatform( context = context, conTentType =
"*/*", uri = uri, packageName = packageName, component = component ) } }}
使用
//分享文字 SharedUtils.i.shareText(context, "分享文字", "我就是想分享一点儿东西")//分享文字到微信SharedUtils.i.shareText( context, text = "我就是想分享一点儿东西", packageName = "com.tencent.mm")
特别声明
本文仅代表作者观点,不代表本站立场,本站仅提供信息存储服务。