zoukankan      html  css  js  c++  java
  • 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> 

    	@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"));
    	      }
    	   }

  • 相关阅读:
    【Redfin SDE intern】跪经
    刷题upupup【Java中Queue、Stack、Heap用法总结】
    刷题upupup【Java中HashMap、HashSet用法总结】
    【lintcode】二分法总结 II
    【lintcode】 二分法总结 I
    201671010117 2016-2017-2 《Java程序设计》面向对象程序设计课程学习进度条
    201671010117 2016-2017-2《Java程序设计》第十八周学习心得
    201671010117 2016-2017-2 《Java程序设计》Java第十七周学习心得
    201671010117 2016-2017-2 《JAVA程序设计》java第十六周学习心得
    201671010117 2016-2017-2 《JAVA程序设计》java第十五周学习心得
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6468720.html
Copyright © 2011-2022 走看看