Android custom system sharing panel, android custom sharing

Source: Internet
Author: User

Android custom system sharing panel, android custom sharing

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:

<?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" />  </menu> 

@ 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 initincluintent () {Intent intent = new Intent (Intent. ACTION_SEND); intent. setType ("text/plain"); List <ResolveInfo> resInfo = getPackageManager (). queryIntentActivities (intent, 0); if (! ResInfo. isEmpty () {List <Intent> targetedincluintents = new ArrayList <Intent> (); 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<ResolveInfo> 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"));      }   }

Related Article

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.