How to implement sharing function in Android

How to implement sharing function in Android

[[169775]]

I have become more and more lazy recently, even typing is too lazy. Recently someone asked me which is better to use for Android sharing function, using Android's own Intent to share or using a third party. Here is the code directly:

1. Use Intent to communicate directly with third-party applications:

  1. /**
  2. * Sharing function
  3. *
  4. * @param context
  5. * Context
  6. * @param activityTitle
  7. * Activity name
  8. * @param msgTitle
  9. * Message title
  10. * @param msgText
  11. * Message content
  12. * @param imgPath
  13. * Image path, pass null if not sharing an image   
  14. */
  15. public void shareUtil(String activityTitle, String msgTitle, String msgText,
  16. String imgPath) {
  17. Intent intent = new Intent(Intent.ACTION_SEND);
  18. if (imgPath == null || imgPath.equals( "" )) {
  19. intent.setType( "text/plain" ); // Plain text
  20. } else {
  21. File f = new File(imgPath);
  22. if (f != null && f.exists() && f.isFile()) {
  23. intent.setType( "image/jpg" );
  24. Uri u = Uri.fromFile(f);
  25. intent.putExtra(Intent.EXTRA_STREAM, u);
  26. }
  27. }
  28. intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
  29. intent.putExtra(Intent.EXTRA_TEXT, msgText);
  30. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  31. startActivity(Intent.createChooser(intent, activityTitle));
  32. }

The code is simple, but to share successfully, the current device must have WeChat installed. Although WeChat is widely used, we must be on guard against accidents. In addition, its sharing is limited, and it can only share text and pictures at most. It is difficult to upgrade the application.

2. Use third-party sharing tools, such as Umeng and shareSDK (supports multiple social platforms).

And it does not require the device to have WeChat installed. If you have used a third party before, you should know how to use it. You can check the corresponding access document for details. It looks like a high-end third party and is very convenient to use, but a third party is a third party after all, you know.

3. Use WeChat's official SDK package. This method does not require WeChat to be installed on the device.

Its implementation method is the same as the second one. After all, it also connects to the third generation. Although it is also a third party, I think Tencent is more reliable. Besides, the ultimate purpose of our sharing is that the most commonly used platforms are no more than this. I will not introduce how to access it in detail here, but just talk about the access skills and common problems (if you are a novice, haha, don’t mess around with the access steps below).

1. Build your own application (ShareTestDemo)

After the application is built, run the program first. Please note that when you run it, it is signed with the Android default debug.keystore.

2. Download the development toolkit from the official website of WeChat Development Platform http://open.weixin.qq.com/download/?lang=zh_CN, unzip it, copy libammsdk.jar to libs, and add it to the build path.

3. Go to the official website of WeChat development platform http://open.weixin.qq.com/agreement to create your application. Those who have used third-party applications know what it means, so the previous basic operations are omitted here (if you don’t understand, you can go to the platform https://open.weixin.qq.com/cgi-bin/frame?t=home/app_tmpl&lang=zh_CN to see the access process). Here you should pay attention to the following steps.

One of the steps is to use the WeChat apk tool to sign our program. Since our program has been installed, we can download and install it directly, then enter the package name in the box on the phone (the package name cannot be wrong), click the Generate button, and an md5 encrypted value will be generated. Enter it into the box on the Create Application page (please note that if our application has not been installed on the phone, the signing tool will prompt that the relevant package is not found).

Next, enter the package name (the package name must not be wrong), and then submit it for review. The next step is to wait for WeChat official review. During the review period, we get the appid provided by WeChat to create the application and connect it to our own application (according to the access document or refer to the DEMO provided by WeChat). This basically completes more than half of the work. The next step is to wait for the review to pass before it can be used normally.

4. After the above process is completed, our application has actually realized the sharing function. At least the test has passed. However, once we use the official signature file to package, install, and publish, we find that the sharing function cannot be used. The WeChat sharing API cannot be adjusted. Don't panic at this time. Go back to the first step. We said that we need to pay attention to "Note that when you run it, you use the Android default debug.keystore signature." This sentence is right. It is the problem caused by the signature file. Pay special attention to this problem when using a third party, including WeChat Alipay. It is the same. Find the problem, and the next step is not simple (when we apply for AppId, we fill in the application signature, which is also generated by debug.keystore. Use our official signature file xxx.keystore to formally package the program, and then install it on the phone, and then run the WeChat signature apk tool again, enter our package name into it (the package name cannot be filled in incorrectly), regenerate an MD5 encryption value, and then go to the official website http://open.weixin.qq.com/agreement, find the created application, click Modify, enter the signature just generated into it, and then save and resubmit for review. )After the review is passed, if we want to use the sharing function, we must formally package and install it.

5. After WeChat sharing is successful, please pay attention to the following two points regarding the WeChat sharing information result feedback, otherwise you will not receive the sharing feedback. (Payment and other operations must pay attention to this)

(1) The name of this Activity must be WXEntryActivity

(2) The WXEntryActivity class must be created under the application package name + wxapi package

<<:  The future of virtual reality: multi-sensory interaction technology

>>:  How to Use Push Notifications in iOS 10

Recommend

Uber's self-driving truck completes first delivery

Foreign media reported that in the early morning ...

These data are quite "hardcore"!

On March 8, the second "Minister's Chann...

Inventory of the latest Weibo advertising resources in 2018!

According to the recent "2018 China Mobile A...

Huawei Hongmeng continues to open up: Ark JS runtime is officially open source

[[422334]] In order to survive in the gap between...

WhatsApp will no longer support Android 4.0.4 and older devices from November 1

[[422900]] WhatsApp, an instant messaging app own...

A successful H5 should hit the user's key points and achieve the operation purpose

Without discussing how to define “success”, can y...