標籤:android 系統分享 自訂分享
在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>
@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, "這是測試分享面板, 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")); } }
Android自訂系統分享面板