Android实现原生分享功能

时间:2021-05-19

本文实例为大家分享了Android实现分享功能的具体代码,供大家参考,具体内容如下

因为公司的需求,最近一直在做分享这一块的功能。大概有这样几种思路:

1.使用Intent调用andoird原生的分享功能;

2.使用第三方的sdk,比如ShareSdk或者友盟;

3.去对应的平台下载jar包,参考官方设计文档写出自己的分享demo,但这种一般也比较复杂,尤其搞不懂qq和微信一家公司的,为什么微信那么麻烦。

不废话了,直接上代码:

一. 新建ShareUtil.java类

import java.io.File; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.text.TextUtils; import android.widget.Toast; public class ShareUtil { private Context context; public ShareUtil(Context context) { this.context = context; } public static final String WEIXIN_PACKAGE_NAME = ""; public static final String QQ_PACKAGE_NAME = ""; // public static final String ; /** * 分享文字 * @param packageName * @param content * @param title * @param subject */ public void shareText(String packageName,String className,String content,String title,String subject){ Intent intent =new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); // if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){ // // }else { // if(null != packageName && !TextUtils.isEmpty(packageName)){ // intent.setPackage(packageName); // } // } if(stringCheck(className) && stringCheck(packageName)){ ComponentName componentName = new ComponentName(packageName, className); intent.setComponent(componentName); }else if(stringCheck(packageName)){ intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_TEXT, content); if(null != title && !TextUtils.isEmpty(title)){ intent.putExtra(Intent.EXTRA_TITLE, title); } if(null != subject && !TextUtils.isEmpty(subject)){ intent.putExtra(Intent.EXTRA_SUBJECT, subject); } intent.putExtra(Intent.EXTRA_TITLE, title); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); } /** * 分享网页 */ public void shareUrl(String packageName,String className,String content,String title,String subject){ Intent intent =new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); // if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){ // // }else { // if(null != packageName && !TextUtils.isEmpty(packageName)){ // intent.setPackage(packageName); // } // } if(stringCheck(className) && stringCheck(packageName)){ ComponentName componentName = new ComponentName(packageName, className); intent.setComponent(componentName); }else if(stringCheck(packageName)){ intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_TEXT, content); if(null != title && !TextUtils.isEmpty(title)){ intent.putExtra(Intent.EXTRA_TITLE, title); } if(null != subject && !TextUtils.isEmpty(subject)){ intent.putExtra(Intent.EXTRA_SUBJECT, subject); } intent.putExtra(Intent.EXTRA_TITLE, title); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); } /** * 分享图片 */ public void shareImg(String packageName,String className,File file){ if(file.exists()){ Uri uri = Uri.fromFile(file); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("image/*"); if(stringCheck(packageName) && stringCheck(className)){ intent.setComponent(new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_STREAM, uri); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); }else { Toast.makeText(context, "文件不存在", 1000).show(); } } /** * 分享音乐 */ public void shareAudio(String packageName,String className,File file){ if(file.exists()){ Uri uri = Uri.fromFile(file); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("audio/*"); if(stringCheck(packageName) && stringCheck(className)){ intent.setComponent(new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_STREAM, uri); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); }else { Toast.makeText(context, "文件不存在", 1000).show(); } } /** * 分享视频 */ public void shareVideo(String packageName,String className,File file){ setIntent("video/*", packageName, className, file); } public void setIntent(String type,String packageName,String className,File file){ if(file.exists()){ Uri uri = Uri.fromFile(file); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType(type); if(stringCheck(packageName) && stringCheck(className)){ intent.setComponent(new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_STREAM, uri); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); }else { Toast.makeText(context, "文件不存在", 1000).show(); } } /** * 分享多张图片和文字至朋友圈 * @param title * @param packageName * @param className * @param file 图片文件 */ public void shareImgToWXCircle(String title,String packageName,String className, File file){ if(file.exists()){ Uri uri = Uri.fromFile(file); Intent intent = new Intent(); ComponentName comp = new ComponentName(packageName, className); intent.setComponent(comp); intent.setAction(Intent.ACTION_SEND); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.putExtra("Kdescription", title); context.startActivity(intent); }else{ Toast.makeText(context, "文件不存在", Toast.LENGTH_LONG).show(); } } /** * 是否安装分享app * @param packageName */ public boolean checkInstall(String packageName){ try { context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (NameNotFoundException e) { e.printStackTrace(); Toast.makeText(context, "请先安装应用app", 1500).show(); return false; } } /** * 跳转官方安装网址 */ public void toInstallWebView(String url){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); context.startActivity(intent); } public static boolean stringCheck(String str){ if(null != str && !TextUtils.isEmpty(str)){ return true; }else { return false; } } }

二. MainActivity.java类

import java.io.File; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { Button btnQQ; Button btnWX; Button btnMore; Button btnWxFriendText; Button btnQQFriendText; Button btnWxFriendImg; Button btnQQFriendImg; Button btnWxFriendAudio; Button btnQQFriendAduio; Button btnWxFriendVideo; Button btnQQFriendVideo; ShareUtil shareUtil; private Button btn_wxCircle_img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnQQ = (Button) findViewById(R.id.btn_qq); btnWX = (Button) findViewById(R.id.btn_wx); btnMore = (Button) findViewById(R.id.btn_more); btnWxFriendText = (Button) findViewById(R.id.btn_wxFriend); btnQQFriendText = (Button) findViewById(R.id.btn_qqFriend); btnWxFriendImg = (Button) findViewById(R.id.btn_wxFriend_img); btnQQFriendImg = (Button) findViewById(R.id.btn_qqFriend_img); btnWxFriendAudio = (Button) findViewById(R.id.btn_wxFriend_audio); btnQQFriendAduio = (Button) findViewById(R.id.btn_qqFriend_audio); btnWxFriendVideo = (Button) findViewById(R.id.btn_wxFriend_video); btnQQFriendVideo = (Button) findViewById(R.id.btn_qqFriend_video); btn_wxCircle_img = (Button) findViewById(R.id.btn_wxCircle_img); btnQQ.setOnClickListener(this); btnWX.setOnClickListener(this); btnMore.setOnClickListener(this); btnWxFriendText.setOnClickListener(this); btnQQFriendText.setOnClickListener(this); btnWxFriendImg.setOnClickListener(this); btnQQFriendImg.setOnClickListener(this); btnWxFriendAudio.setOnClickListener(this); btnQQFriendAduio.setOnClickListener(this); btnWxFriendVideo.setOnClickListener(this); btnQQFriendVideo.setOnClickListener(this); btn_wxCircle_img.setOnClickListener(this); shareUtil = new ShareUtil(this); } @Override public void onClick(View v) { String testImgPath = "/storage/emulated/legacy/display-client/picture/my.png"; String testImagePath = Environment.getExternalStorageDirectory() + "/img.jpg"; String testAudioPath = Environment.getExternalStorageDirectory() + "/audio.mp3"; String testVideoPath = Environment.getExternalStorageDirectory() + "/video.mp4"; File file = new File(testImgPath); File fileImage = new File(testImagePath); File fileAudio = new File(testAudioPath); File fileVideo = new File(testVideoPath); switch (v.getId()) { // qq&文字 case R.id.btn_qq: shareUtil.shareText("com.tencent.mobileqq", null, "这是一条分享信息", "分享标题", "分享主题"); break; // 微信&文字 case R.id.btn_wx: shareUtil.shareText("com.tencent.mm", null, "这是一条分享信息", "分享标题", "分享主题"); break; // 所有&文字 case R.id.btn_more: shareUtil.shareText(null, null, "这是一条分享信息", "分享标题", "分享主题"); break; // 微信朋友&文字 case R.id.btn_wxFriend: if (shareUtil.checkInstall("com.tencent.mm")) { shareUtil.shareText("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", "http://.ai.ipu.share_inent.MainActivity" > <Button android:id="@+id/btn_qq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="qq"/> <Button android:id="@+id/btn_wx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="wx" android:layout_below="@+id/btn_qq"/> <Button android:id="@+id/btn_more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="more" android:layout_below="@+id/btn_wx"/> <Button android:id="@+id/btn_wxFriend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_more" android:text="wxFriendText"/> <Button android:id="@+id/btn_qqFriend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_wxFriend" android:text="qqFriendText" /> <Button android:id="@+id/btn_wxFriend_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_more" android:layout_toRightOf="@+id/btn_wxFriend" android:text="wxFriendImg" /> <Button android:id="@+id/btn_qqFriend_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/btn_qqFriend" android:layout_below="@+id/btn_wxFriend" android:text="qqFriendImg" /> <Button android:id="@+id/btn_wxFriend_audio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_more" android:layout_toRightOf="@+id/btn_wxFriend_img" android:text="wxFriendAudio" /> <Button android:id="@+id/btn_qqFriend_audio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/btn_qqFriend_img" android:layout_below="@+id/btn_wxFriend" android:text="qqFriendAudio" /> <Button android:id="@+id/btn_wxFriend_video" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_qqFriend" android:text="wxFriendVideo" /> <Button android:id="@+id/btn_qqFriend_video" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_wxFriend_video" android:text="qqFriendVideo" /> <Button android:id="@+id/btn_wxCircle_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_wxFriend_video" android:layout_toRightOf="@+id/btn_wxFriend_img" android:text="wxCircleImg" /> </RelativeLayout>

其中微信的分享只能分享文字和图片,不能单独分享图片或者文字。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章