Sharing in Android is a convenient way to share our app by calling the system's share panel. The most basic implementation is as follows:
Public Intent getshareintent () {Intent Intent = new Intent (); intent.setaction (intent.action_send); Intent.putextra ( Intent.extra_text, "This is the test sharing panel, Http://www.baidu.comss"); Intent.settype ("Text/plain"); return Intent;}
There is also the implementation of the Actionbar on the Add share list, the implementation code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/menu_item_share" android:showasaction= "ifroom" android:title= "Share" android:actionproviderclass= "Android.widget.ShareActionProvider"/>
@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.actionbar_menu, menu); MenuItem item = Menu.finditem (R.id.menu_item_share); shareactionprovider = (Shareactionprovider) Item.getactionprovider (); Intent shareintent = Getshareintent (); shareactionprovider.setshareintent (shareIntent); return true;} Public Intent getshareintent () {Intent Intent = new Intent (); intent.setaction (intent.action_send); Intent.putextra ( Intent.extra_text, "This is the test sharing panel, Http://www.baidu.comss"); Intent.settype ("Text/plain"); return Intent;}
The system will find all the apps that support the Setetype type by default, and we can also implement a custom sharing platform.
private void Initshareintent () {Intent Intent = new Intent (intent.action_send); Intent.settype ("Text/plain"); list<resolveinfo> resinfo = Getpackagemanager (). Queryintentactivities (Intent, 0); if (!resinfo.isempty ()) {List <Intent> targetedshareintents = new arraylist<intent> (); for (ResolveInfo Info:resinfo) {Intent targeted = NE W Intent (Intent.action_send); Targeted.settype ("Text/plain"); Activityinfo Activityinfo = info.activityinfo;//Here can add the appropriate platform, with | | Connect if (activityInfo.packageName.contains ("com.tencent.mm")) {Targeted.putextra (Intent.extra_text, "Share content"); Targeted.setpackage (Activityinfo.packagename); Targetedshareintents.add (targeted);}} Intent chooserintent = intent.createchooser (targetedshareintents.remove (0), "Select app to share"); if (chooserintent = = NULL) {return;} Chooserintent.putextra (Intent.extra_initial_intents,targetedshareintents.toarray (new Parcelable[] {})), try { StartActivity (chooserintent);} catch (Android.content.ActivityNotFoundException ex) {Toast.maketeXT (This, "Can ' t find sharecomponent to share", Toast.length_short). Show ();}}
There are some flaws in the system's sharing panel, such as the different styles of the panels displayed on each phone, and the different types and numbers of sharing platforms displayed on different phones, and there will be some messy applications. We can add parameters to the above method, so that we can only share to a platform to solve this problem, so we can customize a sharing panel to add the application we want, the code is as follows:
private void Initshareintent (String type) {Boolean found = false; Intent share = new Intent (Android.content.Intent.ACTION_SEND); Share.settype ("image/*"); Gets the list of intentsthat can be loaded. List<resolveinfo> resinfo =getpackagemanager (). Queryintentactivities (share, 0); if (!resinfo.isempty ()) {for (ResolveInfo info:resinfo) {if (info.activityInfo.packageName.toLowerCas E (). Contains (type) | | Info.activityInfo.name.toLowerCase (). Contains (type)) {Share.putextra (Intent.extra_subject, "SUBJECT"); Share.putextra (Intent.extra_text, "your TEXT"); Share.putextra (Intent.extra_stream,//Uri.fromfile (NewFile (MyPath))); Optional, just///If you wanna////share AN////image. Share.setpackage (Info.activityInfo.packageName); Found = true; Break } } if (!found) return; StartActivity (Intent.createchooser (Share, "select")); } }
Android Custom system sharing panel