This article has shared the specific code for Android sharing function for your reference. The specific content is as follows
Due to the company's needs, I have been working on sharing this function recently. There are probably several ideas:
1. Use Intent to call andoird's native sharing function;
2. Use third-party SDKs, such as ShareSdk or Youmeng;
3. Go to the corresponding platform to download the jar package and refer to the official design document to write your own sharing demo, but this is generally more complicated, especially if you don’t understand the QQ and WeChat company, why WeChat is so troublesome.
No more nonsense, just upload the code:
1. Create a new class
import ; import ; import ; import ; import ; import ; import ; import ; import ; public class ShareUtil { private Context context; public ShareUtil(Context context) { = context; } public static final String WEIXIN_PACKAGE_NAME = ""; public static final String QQ_PACKAGE_NAME = ""; // public static final String ; /** * Share text * @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.ACTION_SEND); ("text/plain"); // if(null != className && null != packageName && !(className) && !(packageName)){ // // }else { // if(null != packageName && !(packageName)){ // (packageName); // } // } if(stringCheck(className) && stringCheck(packageName)){ ComponentName componentName = new ComponentName(packageName, className); (componentName); }else if(stringCheck(packageName)){ (packageName); } (Intent.EXTRA_TEXT, content); if(null != title && !(title)){ (Intent.EXTRA_TITLE, title); } if(null != subject && !(subject)){ (Intent.EXTRA_SUBJECT, subject); } (Intent.EXTRA_TITLE, title); Intent chooserIntent = (intent, "Share to:"); (chooserIntent); } /** * Share the web page */ public void shareUrl(String packageName,String className,String content,String title,String subject){ Intent intent =new Intent(); (Intent.ACTION_SEND); ("text/plain"); // if(null != className && null != packageName && !(className) && !(packageName)){ // // }else { // if(null != packageName && !(packageName)){ // (packageName); // } // } if(stringCheck(className) && stringCheck(packageName)){ ComponentName componentName = new ComponentName(packageName, className); (componentName); }else if(stringCheck(packageName)){ (packageName); } (Intent.EXTRA_TEXT, content); if(null != title && !(title)){ (Intent.EXTRA_TITLE, title); } if(null != subject && !(subject)){ (Intent.EXTRA_SUBJECT, subject); } (Intent.EXTRA_TITLE, title); Intent chooserIntent = (intent, "Share to:"); (chooserIntent); } /** * Share pictures */ public void shareImg(String packageName,String className,File file){ if(()){ Uri uri = (file); Intent intent = new Intent(); (Intent.ACTION_SEND); ("image/*"); if(stringCheck(packageName) && stringCheck(className)){ (new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { (packageName); } (Intent.EXTRA_STREAM, uri); Intent chooserIntent = (intent, "Share to:"); (chooserIntent); }else { (context, "The file does not exist", 1000).show(); } } /** * Share music */ public void shareAudio(String packageName,String className,File file){ if(()){ Uri uri = (file); Intent intent = new Intent(); (Intent.ACTION_SEND); ("audio/*"); if(stringCheck(packageName) && stringCheck(className)){ (new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { (packageName); } (Intent.EXTRA_STREAM, uri); Intent chooserIntent = (intent, "Share to:"); (chooserIntent); }else { (context, "The file does not exist", 1000).show(); } } /** * Share video */ 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(()){ Uri uri = (file); Intent intent = new Intent(); (Intent.ACTION_SEND); (type); if(stringCheck(packageName) && stringCheck(className)){ (new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { (packageName); } (Intent.EXTRA_STREAM, uri); Intent chooserIntent = (intent, "Share to:"); (chooserIntent); }else { (context, "The file does not exist", 1000).show(); } } /** * Share multiple pictures and texts to your circle of friends * @param title * @param packageName * @param className * @param file Picture file */ public void shareImgToWXCircle(String title,String packageName,String className, File file){ if(()){ Uri uri = (file); Intent intent = new Intent(); ComponentName comp = new ComponentName(packageName, className); (comp); (Intent.ACTION_SEND); ("image/*"); (Intent.EXTRA_STREAM, uri); ("Kdescription", title); (intent); }else{ (context, "The file does not exist", Toast.LENGTH_LONG).show(); } } /** * Whether to install the sharing app * @param packageName */ public boolean checkInstall(String packageName){ try { ().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (NameNotFoundException e) { (); (context, "Please install the app first", 1500).show(); return false; } } /** * Jump to the official installation website */ public void toInstallWebView(String url){ Intent intent = new Intent(); (Intent.ACTION_VIEW); ((url)); (intent); } public static boolean stringCheck(String str){ if(null != str && !(str)){ return true; }else { return false; } } }
II. Class
import ; import ; import ; import ; import ; import ; import ; 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) { (savedInstanceState); setContentView(.activity_main); btnQQ = (Button) findViewById(.btn_qq); btnWX = (Button) findViewById(.btn_wx); btnMore = (Button) findViewById(.btn_more); btnWxFriendText = (Button) findViewById(.btn_wxFriend); btnQQFriendText = (Button) findViewById(.btn_qqFriend); btnWxFriendImg = (Button) findViewById(.btn_wxFriend_img); btnQQFriendImg = (Button) findViewById(.btn_qqFriend_img); btnWxFriendAudio = (Button) findViewById(.btn_wxFriend_audio); btnQQFriendAduio = (Button) findViewById(.btn_qqFriend_audio); btnWxFriendVideo = (Button) findViewById(.btn_wxFriend_video); btnQQFriendVideo = (Button) findViewById(.btn_qqFriend_video); btn_wxCircle_img = (Button) findViewById(.btn_wxCircle_img); (this); (this); (this); (this); (this); (this); (this); (this); (this); (this); (this); btn_wxCircle_img.setOnClickListener(this); shareUtil = new ShareUtil(this); } @Override public void onClick(View v) { String testImgPath = "/storage/emulated/legacy/display-client/picture/"; String testImagePath = () + "/"; String testAudioPath = () + "/audio.mp3"; String testVideoPath = () + "/video.mp4"; File file = new File(testImgPath); File fileImage = new File(testImagePath); File fileAudio = new File(testAudioPath); File fileVideo = new File(testVideoPath); switch (()) { // qq&text case .btn_qq: ("", null, "This is a sharing message", "Share Title", "Share topic"); break; // WeChat & text case .btn_wx: ("", null, "This is a sharing message", "Share Title", "Share topic"); break; // All & text case .btn_more: (null, null, "This is a sharing message", "Share Title", "Share topic"); break; // WeChat friends & text case .btn_wxFriend: if (("")) { ("", "", "/", "Share Title", "Share topic"); } else { ("/download"); } break; // qq friends & text case .btn_qqFriend: if (("")) { ("", "", "/", "Share Title", "Share topic"); } else { ("/mobileqq/"); } break; // WeChat friends & pictures case .btn_wxFriend_img: ("", "", fileImage); break; // qq friends & pictures case .btn_qqFriend_img: ("", "", fileImage); break; case .btn_wxFriend_audio: ("", "", fileAudio); break; case .btn_qqFriend_audio: ("", "", fileAudio); break; case .btn_wxFriend_video: ("", "", fileVideo); break; case .btn_qqFriend_video: ("", "", fileVideo); break; case .btn_wxCircle_img: ("Dog Pictures", "", "", fileImage); break; } } }
3. Layout file activity_main.xml
<RelativeLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".share_inent.MainActivity" > <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="qq"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="wx" android:layout_below="@+id/btn_qq"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="more" android:layout_below="@+id/btn_wx"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_more" android:text="wxFriendText"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_wxFriend" android:text="qqFriendText" /> <Button android: 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: 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: 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: 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: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_qqFriend" android:text="wxFriendVideo" /> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_wxFriend_video" android:text="qqFriendVideo" /> <Button android: 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>
Among them, WeChat sharing can only share text and pictures, and cannot share pictures or text separately.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.