Share panel of Android custom system
There is a convenient way to implement sharing in Android. Call the sharing Panel of the system to share our applications. The basic implementation is as follows:
Public Intent getincluintent () {Intent intent = new Intent (); intent. setAction (Intent. ACTION_SEND); intent. putExtra (Intent. EXTRA_TEXT, "This is the test share panel, http://www.baidu.comss"); intent. setType ("text/plain"); return intent ;}
Another method is to add a sharing list on the ActionBar. The implementation code is as follows:
@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. actionbar_menu, menu); MenuItem item = menu. findItem (R. id. menu_item_share); required actionprovider = (required actionprovider) item. getActionProvider (); Intent = getintent Intent (); Response actionprovider. setincluintent (incluintent); return true;} public Intent getincluintent () {Intent intent = new Intent (); intent. setAction (Intent. ACTION_SEND); intent. putExtra (Intent. EXTRA_TEXT, "This is the test share panel, http://www.baidu.comss"); intent. setType ("text/plain"); return intent ;}
By default, the system will find all applications that support the seteType type. We can also customize the sharing platform.
Private void initShareIntent () {Intent intent = new Intent (Intent. ACTION_SEND); intent. setType ("text/plain"); List
ResInfo = getPackageManager (). queryIntentActivities (intent, 0); if (! ResInfo. isEmpty () {List
TargetedShareIntents = new ArrayList
(); For (ResolveInfo info: resInfo) {Intent targeted = new Intent (Intent. ACTION_SEND); targeted. setType ("text/plain"); ActivityInfo activityInfo = info. activityInfo; // you can add a platform here. Use | to connect to if (activityInfo. packageName. contains ("com. tencent. mm ") {targeted. putExtra (Intent. EXTRA_TEXT, "shared 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 your component to share", Toast. LENGTH_SHORT ). show ();}}}
The sharing Panel of the system has some defects. For example, the display panel of each mobile phone has different styles. Different types and numbers of sharing platforms are displayed on different mobile phones, which may lead to messy applications. We can add parameters to the above method so that we can solve this problem only by sharing them on one platform. In this way, we can customize a sharing panel to add the desired application, 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
resInfo =getPackageManager().queryIntentActivities( share, 0); if (!resInfo.isEmpty()) { for (ResolveInfo info : resInfo) { if (info.activityInfo.packageName.toLowerCase().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")); } }