• 安卓,通过本地应用分享到微信、facebook等


    别的不说了,直接上代码。

    支持分享到微信、微博、facebook、twitter

    package com.example.shareSample;
    
    import java.util.List;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.pm.ResolveInfo;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    	protected void onCreate(Bundle paramBundle) {
    		super.onCreate(paramBundle);
    		setContentView(R.layout.main);
    		((Button) findViewById(R.id.toFacebookButton))
    				.setOnClickListener(new View.OnClickListener() {
    					public void onClick(View paramAnonymousView) {
    						Toast.makeText(
    								MainActivity.this.getApplicationContext(),
    								"waiting to share to facebook",
    								Toast.LENGTH_SHORT).show();
    						MainActivity.this.shareToFacebook();
    					}
    				});
    		((Button) findViewById(R.id.toTwitterButton))
    				.setOnClickListener(new View.OnClickListener() {
    					public void onClick(View paramAnonymousView) {
    						Toast.makeText(
    								MainActivity.this.getApplicationContext(),
    								"waiting to share to twitter",
    								Toast.LENGTH_SHORT).show();
    						MainActivity.this.shareToTwitter();
    					}
    				});
    		((Button) findViewById(R.id.toWeixinButton))
    				.setOnClickListener(new View.OnClickListener() {
    					public void onClick(View paramAnonymousView) {
    						Toast.makeText(
    								MainActivity.this.getApplicationContext(),
    								"waiting to share to weixin",
    								Toast.LENGTH_SHORT).show();
    						MainActivity.this.shareToWeixin();
    					}
    				});
    		((Button) findViewById(R.id.toWeiboButton))
    				.setOnClickListener(new View.OnClickListener() {
    					public void onClick(View paramAnonymousView) {
    						Toast.makeText(
    								MainActivity.this.getApplicationContext(),
    								"waiting to share to weibo", Toast.LENGTH_SHORT)
    								.show();
    						MainActivity.this.shareToWeibo();
    					}
    				});
    	}
    	
    	private void shareToFacebook() {
    		shareToByType("facebook");
    	}
    
    	private void shareToTwitter() {
    		shareToByType("twitter");
    	}
    
    	private void shareToWeibo() {
    		shareToByType("weibo");
    	}
    
    	private void shareToWeixin() {
    		Intent localIntent = new Intent();
    		localIntent.setComponent(new ComponentName("com.tencent.mm",
    				"com.tencent.mm.ui.tools.ShareImgUI"));
    		localIntent.setAction("android.intent.action.SEND");
    		localIntent.setType("image/*");
    		localIntent.putExtra("android.intent.extra.TEXT", "测试消息");
    		startActivity(localIntent);
    	}
    
    	private void shareToByType(String type) {
    		boolean found = false;
    		Intent share = new Intent(android.content.Intent.ACTION_SEND);
    		share.setType("image/jpeg");
    		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.setPackage(info.activityInfo.packageName);
    					found = true;
    					break;
    				}
    			}
    			if (!found)
    				return;
    
    			startActivity(Intent.createChooser(share, "Select"));
    		}
    	}
    
    	
    
    }
    

  • 相关阅读:
    Chrome浏览器设置默认编码
    linux上安装subversion
    详解Linux命令行下常用svn命令
    css 使容器宽度适应内容宽
    Windsor Spring
    T4 Generate POCO Class for MSSQ
    MSSQ 树型结构数据 循环操作
    System.Reflection.Emit 动态实现接口
    T4 SqlSugar MySql
    微信多开
  • 原文地址:https://www.cnblogs.com/jerry1999/p/4175907.html
Copyright © 2020-2023  润新知