Many applications are now basically integrated to share this feature, which includes system sharing (such as mail, SMS) and third-party sharing (such as QQ and). Some of these companies will choose to use a third-party library to simplify these operations, speed up development, with more than the friends of the social sharing SDK, the disadvantage is that the degree of freedom is too low, because you may just need QQ and other companies will choose to import the required third-party SDK to customize the sharing function, high degree of freedom, So this blog mainly to introduce the latter a custom sharing function of the case demo, is the demo running effect:
Specific analysis of the source, because the content of the share according to the needs of different, for the sake of simplicity, we use the most commonly used graphics + link form as an example, other circumstances how to modify the relevant classes according to needs. The first step is to define the shared entity class:
Sharemodel.java
public class sharemodel { /** share type, picture, text, etc. */ public int type; /** share title */ public String title; /** share content */ public String content; /** Shared links */ public String Shareurl; /** network URL to share images */ public Arraylist<string> ImageUrl; /** share Picture's local path path */ public Arraylist<string> ImagePath;}
This class defines the basic data types for sharing (modify the class as needed, such as the ability to add a video voice, and so on). We then set up separate sharing modules to differentiate between sharing (this is the case with system and QQ sharing), and each sharing module inherits from the same interface Ishare:
Ishare.java
Public interface ishare { /** * @param model Call shared entity * @param Context context * @param type Call the shared category *
@param Callback share callback *
/ voidDoshare (Sharemodel model, context context,intType, Isharecallback callback);/** * This function is called in {@link android.app.activity#onactivityresult (int, int, Intent)} * To handle shared callbacks, such as QQ and system sharing (this does not need to be in the Onact Ivityresult directly return false) * @return is the shared callback */ BooleanDosharecallback (intRequestcode,intResultCode, Intent data);}
The interface defines two functions, one is the share function, the other is the shared callback function, the system share and QQ share related classes inherit the interface implementation of these two functions:
QQ share Tencentshare. class
public class tencentshare implements ishare { @Override public void doshare (Sharemodel model, context context, int type, Isharecallback callback) {//concrete View Source } @Override public Boolean dosharecallback (int Requestcode , int ResultCode, Intent data) {//concrete View Source }}< /code>
System sharing Systemshare.class
public class systemshare implements ishare { @Override public void doshare (Sharemodel model, context context, int type, Isharecallback callback) {//concrete View Source } @Override public Boolean dosharecallback (int Requestcode , int ResultCode, Intent data) {//concrete View Source }}< /code>
If you need to integrate with other third-party sharing, create a new related class to implement the relevant functions. Then is the most important management class, look at the source of the class:
Public class sharemanager { PrivateActivity activity;PrivateIsharecallback callback;PrivatePopupwindow Popupwindow;/** latest call to Shared object * / PrivateIshare Shareobject;/** * @param callback to share the callback * * Public Sharemanager(Activity activity, Isharecallback callback) { This. activity = activity; This. callback = callback;//Initialize the Share enumeration classShare demo = Share.qq_friend; }/** * Used to show shared Popupwindow * @param Model Shared entities */ Public void Show(FinalSharemodel model) {if(Popupwindow = =NULL) {Popupwindow =NewSharegridviewpopupwindow (activity,NewSharegridviewpopupwindow.ishareclickcallback () {@Override Public void Onsharecallback(intPosition) {Try{class< extends ishare> clazz = Share.values () [Position].getshareclass (); Shareobject = Clazz.newinstance (); Shareobject.doshare (model, activity, share.values () [Position].gettype (), callback); }Catch(Instantiationexception e) {E.printstacktrace (); }Catch(Illegalaccessexception e) {E.printstacktrace (); } } }); Popupwindow.setondismisslistener (NewPopupwindow.ondismisslistener () {@Override Public void Ondismiss() {Popupwindow =NULL; } }); } popupwindow.showatlocation (Activity.getwindow (). Getdecorview (), Gravity.bottom | Gravity.center_horizontal,0,0); }/** * is used to register the share callback in the activity's onactivityresult function, such as QQ and system sharing * @return is the share callback, if yes, returns True, indicating that the activity does not handle the related re Sult * * Public Boolean Registeronactivitycallback(intRequestcode,intResultCode, Intent data) {returnshareobject!=NULL&& Shareobject.dosharecallback (Requestcode, ResultCode, data); }/** * Add share module here dynamically * / Public enumShare {qq_friend ("QQ", R.MIPMAP.LOGO_QQ,0, Tencentshare.class), Qq_zone ("QQ Space", R.mipmap.logo_qzone,1, Tencentshare.class), MAIL ("Mail", R.mipmap.logo_email,0, Systemshare.class), MESSAGE ("Information", R.mipmap.logo_shortmessage,1, Systemshare.class);PrivateString name;Private intdrawable;Privateclass<? Extends ishare> Shareclass;Private intType Share (String name,intDrawable,intType, class<? Extends Ishare> Shareclass) { This. name = name; This. drawable = drawable; This. type = type; This. Shareclass = Shareclass; } PublicStringGetName(){returnName } Public int Getdrawableid(){returndrawable; } Public int GetType(){returnType } Publicclass<? Extends ishare>Getshareclass() {returnShareclass; } }}
The class uses an enum to centrally define all the shares, each share has a name and a picture to show, and the Click Effect callback is handled by invoking the Doshare function on the class object that inherits from the Ishare interface. The benefit of doing so is to facilitate management and subsequent sharing of categories of additions and deletions. There is also a registeronactivitycallback function in the class that is used to register the share callback in the activity's Onactivityresult to notify the associated shared callback of the success or not.
Here we use Popupwindow to pop up the sharing box, so we need to customize a Popupwindow:
Sharegridviewpopupwindow.java class:
Public class sharegridviewpopupwindow extends popupwindow { PrivateView Mmenuview;PrivateLayoutinflater Inflater;PrivateGridView GridView;PrivateTextView Tv_dismiss;PrivateActivity context;PrivateView view; Public Sharegridviewpopupwindow(Activity context,FinalIshareclickcallback callback) {Super(); This. Context = Context; Inflater = (layoutinflater) context.getsystemservice (Service.layout_inflater_service); Mmenuview = Inflater.inflate (R.layout.share_popupwindow,NULL); GridView = (GridView) Mmenuview.findviewbyid (R.id.gridview); Tv_dismiss = (TextView) Mmenuview.findviewbyid (R.id.tv_dismiss); Gridview.setadapter (NewGridviewadapter ()); Gridview.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {if(Callback! =NULL) Callback.onsharecallback (position); Dismiss (); } }); This. Setanimationstyle (R.style.share_popupanimation);//http://stackoverflow.com/questions/3121232/android-popup-window-dismissal This. setbackgrounddrawable (NewBitmapdrawable (NULL,"")); This. setfocusable (true); Tv_dismiss.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {Dismiss (); } }); This. Setcontentview (Mmenuview); This. SetWidth (Layoutparams.match_parent); This. SetHeight (Layoutparams.wrap_content);//Click Popupwindow other parts disappearMmenuview.setontouchlistener (NewOntouchlistener () { Public Boolean OnTouch(View V, motionevent event) {intHeight = Mmenuview.findviewbyid (r.id.pop_layout). GetTop ();inty = (int) event.gety ();if(event.getaction () = = motionevent.action_up) {if(Y < height) {Dismiss (); } }return true; } });//Add gray effect to other parts of PopupwindowView =NewView (context); View.setbackgroundcolor (Color.parsecolor ("#b0000000")); ((ViewGroup) Context.getwindow (). Getdecorview (). Getrootview ()). AddView (view); }Private class gridviewadapter extends baseadapter { @Override Public int GetCount() {returnShareManager.Share.values (). length; }@Override PublicObjectGetItem(intPosition) {returnShareManager.Share.values () [position]; }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {if(Convertview = =NULL) {Convertview = Inflater.inflate (R.layout.share_grid_item,NULL); } Convertview.findviewbyid (R.id.iv_image). Setbackgroundresource (ShareManager.Share.values () [Position].getdrawab LeId ()); ((TextView) Convertview.findviewbyid (R.id.tv_text)). SetText (ShareManager.Share.values () [Position].getname ()];returnConvertview; } }@Override Public void Dismiss() {Super. dismiss (); ((ViewGroup) Context.getwindow (). Getdecorview (). Getrootview ()). Removeview (view); } Public interface ishareclickcallback { voidOnsharecallback (intposition); }}
The Popupwindow uses the GridView to layout, you can modify the layout according to the requirements, about the use of Popupwindow I say here: The 1th is the Popupwindow click the external and return keys disappear processing, Click outside disappear need to use the Setontouchlistener function to monitor the location of the user touch, press the return key processing needs to first use the Setbackgrounddrawable function to set a background for the Popupwindow, Then use the Setfocusable function to set the focus, the 2nd is the outside of the gray background, I use this is when the Popupwindow display when the Decorview to add a transparent black view, in the dismiss time to remove, so that the effect is achieved.
Basic ideas are finished, very simple, specific people can go to see the source:
Https://github.com/zhaozepeng/ShareManager
Android Integrated systems sharing and third-party sharing cases