Android integration system sharing and third-party sharing cases

Source: Internet
Author: User

Android integration system sharing and third-party sharing cases

Nowadays, many applications basically integrate the sharing function, which includes system sharing (such as email, SMS) and third-party sharing (such as QQ and ). Some companies choose to use third-party libraries to simplify these operations and accelerate development. The disadvantage is that the degree of freedom is too low, because you only need QQ and others will choose to import the required third-party SDK to customize the sharing function, with a high degree of freedom, this blog introduces the demo of the last custom sharing function, which is the running effect of the demo:
  
The source code is analyzed in detail. The shared content varies with requirements. For simplicity, we will take the most commonly used text and text + link form as an example, in other cases, how can I modify related classes as needed. Step 1 defines the entity class for sharing:
Using model. java

Public class sharing model {/** shared type, image, text, etc. */public int type;/** shared title */public String title; /** share content */public String content;/** share link */public String url;/** share image url */public ArrayList
  
   
ImageUrl;/** local path of the image to be shared */public ArrayList
   
    
ImagePath ;}
   
  

This class defines the basic data type for sharing (you can modify this class as needed, for example, you can add video and voice ). Next, we will build our own sharing modules to differentiate the sharing (here we will use the system and QQ sharing as an example), and each sharing module will inherit from the same interface IShare:
IShare. java

Public interface IShare {/*** @ param model calls the shared object * @ param context * @ param type calls the shared object category * @ param callback sharing callback */void doShare (using model, context context, int type, IShareCallback callback);/*** the function is in {@ link android. app. activity # onActivityResult (int, int, Intent)} * Call, used to process the share callback, such as QQ and system sharing (this kind of callback does not need to be processed in onActivityResult to directly return false) * @ return indicates whether the callback is shared */boolean doShareCallback (int requestCode, int resultCode, Intent data );}

This interface defines two functions: one is the sharing function and the other is the sharing callback function. The classes related to system sharing and QQ sharing inherit from this interface to implement these two functions:
QQ share TencentShare. class

Public class TencentShare implements IShare {@ Override public void doShare (invalid model, Context context, int type, IShareCallback callback) {// For details, refer to the source code} @ Override public boolean doShareCallback (int requestCode, int resultCode, Intent data) {// view the source code for details }}

System share SystemShare. class

Public class SystemShare implements IShare {@ Override public void doShare (invalid model, Context context, int type, IShareCallback callback) {// For details, see the source code} @ Override public boolean doShareCallback (int requestCode, int resultCode, Intent data) {// view the source code for details }}

If you want to integrate with other third parties, create a related class to implement relevant functions. Then there is the most important management class. Let's look at the source code of this class:

Public class ShareManager {private Activity activity; private IShareCallback callback; private PopupWindow popupWindow;/** the latest shared object called */private IShare jsonobject; /*** @ param callback the callback shared out */public ShareManager (Activity activity, IShareCallback callback) {this. activity = activity; this. callback = callback; // initialize Share Enumeration class Share demo = Share. QQ_FRIEND;}/*** used to display the sharing popUpWindow * @ param mode L shared object */public void show (final ready model) {if (popupWindow = null) {popupWindow = new ShareGridViewPopupWindow (activity, new ShareGridViewPopupWindow. IShareClickCallback () {@ Override public void onShareCallback (int position) {try {Class clazz = Share. values () [position]. getShareClass (); jsonobject = clazz. newInstance (); jsonobject. doShare (model, activity, Share. values () [position ]. GetType (), callback);} catch (InstantiationException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace () ;}}); popupWindow. setOnDismissListener (new PopupWindow. onDismissListener () {@ Override public void onDismiss () {popupWindow = null ;}}) ;} popupWindow. showAtLocation (activity. getWindow (). getDecorView (), Gravity. BOTTOM | Gravity. CENTER_HORIZONTAL, 0, 0 );} /*** Registers the sharing callback in the onActivityResult function of the activity, for example, QQ and system sharing * @ return indicates whether to share the callback. If yes, true is returned, indicates that the activity does not need to process related results */public boolean registerOnActivityCallback (int requestCode, int resultCode, Intent data) {return jsonobject! = Null & jsonobject. doShareCallback (requestCode, resultCode, data);}/*** dynamically Add a sharing module here */public enum Share {QQ_FRIEND ("QQ", R. mipmap. logo_qq, 0, TencentShare. class), QQ_ZONE ("QQ space", R. mipmap. logo_qzone, 1, TencentShare. class), MAIL ("email", R. mipmap. logo_email, 0, SystemShare. class), MESSAGE ("information", R. mipmap. logo_cmdmessage, 1, SystemShare. class); private String name; private int drawable; private Class shareClass; private int type; Share (String name, int drawable, int type, Class shareClass) {this. name = name; this. drawable = drawable; this. type = type; this. shareClass = shareClass;} public String getName () {return name;} public int getDrawableId () {return drawable;} public int getType () {return type;} public Class getShareClass () {return shareClass ;}}}

This class uses an enum to define all the shares in a centralized manner. Each share has a name and an image for display, the click effect callback is handled by calling the doShare function for class objects inherited from the IShare interface, the advantage of doing so is to facilitate management and the addition and deletion of sharing categories in the future. This class also has a registerOnActivityCallback function, which is used to register the sharing callback in the onActivityResult of the Activity to notify whether the sharing callback is successful or not.
Here we use popupWindow to bring up the share box, so we need to customize a popupWindow:
ShareGridViewPopupWindow. java class:

Public class extends PopupWindow {private View mMenuView; private LayoutInflater inflater; private GridView; private TextView TV _dismiss; private Activity context; private View view; public comment (Activity context, final IShareClickCallback callback) {super (); this. context = context; inflater = (LayoutInflater) context. getSystemService (Service. L AYOUT_INFLATER_SERVICE); mMenuView = inflater. inflate (R. layout. pai_popupwindow, null); gridView = (GridView) mMenuView. findViewById (R. id. gridview); TV _dismiss = (TextView) mMenuView. findViewById (R. id. TV _dismiss); gridView. setAdapter (new GridViewAdapter (); gridView. setOnItemClickListener (new AdapterView. onItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int Position, long id) {if (callback! = Null) callback. onShareCallback (position); dismiss () ;}}); this. setAnimationStyle (R. style. performance_popupanimation); // http://stackoverflow.com/questions/3121232/android-popup-window-dismissal this. setBackgroundDrawable (new BitmapDrawable (null, ""); this. setFocusable (true); TV _dismiss.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {dismiss () ;}}); this. setContentView (mMenuView); this. setWidth (LayoutParams. MATCH_PARENT); this. setHeight (LayoutParams. WRAP_CONTENT); // click the other part of popUpWindow to disappear mMenuView. setOnTouchListener (new OnTouchListener () {public boolean onTouch (View v, MotionEvent event) {int height = mMenuView. findViewById (R. id. pop_layout ). getTop (); int y = (int) event. getY (); if (event. getAction () = MotionEvent. ACTION_UP) {if (y 

The popupWindow uses the gridView for layout. You can modify the layout as needed. For the use of popupWindow, here are some points: the first point is the processing of popupWindow clicking the external button and the Return key disappearing, the setOnTouchListener function is used to monitor the position of the user's touch when you click the external button to disappear. to press the return key, you must first use the setBackgroundDrawable function to set a background for the popupWindow and then use the setFocusable function to set focus; the second point is the gray background on the outside. Here I am using a transparent and black view added to the decorView when the popupWindow is displayed. When dismiss is displayed, the effect is achieved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.