在Android分享知道有一个更方便的方法。调用的共享面板来分享我们的应用程序的系统。主要实现例如,下面的:
public Intent getShareIntent(){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, "这是測试分享面板, http://www.baidu.comss"); intent.setType("text/plain"); return intent; }另一种是实如今ActionBar上加入分享列表,实现代码例如以下:
<?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>
@Override public 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, "这是測试分享面板, http://www.baidu.comss"); intent.setType("text/plain"); return intent; }系统默认会为我们找出全部支持seteType中类型的应用。相同我们能够实现自己定义分享的平台。
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 = new Intent(Intent.ACTION_SEND); targeted.setType("text/plain"); ActivityInfo activityInfo = info.activityInfo; //在这里能够加入对应的平台。用 || 连接 if (activityInfo.packageName.contains("com.tencent.mm")) { targeted.putExtra(Intent.EXTRA_TEXT, "分享内容"); 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(); } } }系统的分享面板存在一些缺陷,比方每一个手机显示的面板的样式不同,不同手机上显示的分享平台种类和数目不同,会出现一些杂乱的应用。我们能够给上面的方法加入參数,让仅仅能分享到一个平台就能够解决问题。这样我们就能够自己定义一个分享面板。来加入我们想要的应用,代码例如以下:
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")); } }