1. 程式人生 > >Android原生分享功能的思考與實現

Android原生分享功能的思考與實現

因為公司的需求,最近一直在做分享這一塊的功能。大概有這樣幾種思路:

1.使用Intent呼叫andoird原生的分享功能;

2.使用第三方的sdk,比如ShareSdk或者友盟;

3.去對應的平臺下載jar包,參考官方設計文件寫出自己的分享demo,但這種一般也比較複雜,尤其搞不懂qq和微信一家公司的,為什麼微信那麼麻煩。

不廢話了,直接上程式碼:

一. 新建ShareUtil.java類

<span style="font-size:14px;">import java.io.File;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;
public class ShareUtil {
	private Context context;
	
	public ShareUtil(Context context) {
		this.context = context;
	}
	
	public static final String WEIXIN_PACKAGE_NAME = "";
	public static final String QQ_PACKAGE_NAME = "";
//	public static final String ;
	
	
	/**
	 * 分享文字
	 * @param packageName
	 * @param content
	 * @param title
	 * @param subject
	 */
	public void shareText(String packageName,String className,String content,String title,String subject){
			Intent intent =new Intent();
			intent.setAction(Intent.ACTION_SEND);
			intent.setType("text/plain");
	//		if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){
	//			
	//		}else {			
	//			if(null != packageName && !TextUtils.isEmpty(packageName)){
	//				intent.setPackage(packageName);
	//			}
	//		}
			if(stringCheck(className) && stringCheck(packageName)){
				ComponentName componentName = new ComponentName(packageName, className);
				intent.setComponent(componentName);
			}else if(stringCheck(packageName)){
				intent.setPackage(packageName);
			}
			
			intent.putExtra(Intent.EXTRA_TEXT, content);
			if(null != title && !TextUtils.isEmpty(title)){			
				intent.putExtra(Intent.EXTRA_TITLE, title);
			}
			if(null != subject && !TextUtils.isEmpty(subject)){
				intent.putExtra(Intent.EXTRA_SUBJECT, subject);
			}
			intent.putExtra(Intent.EXTRA_TITLE, title);
			Intent chooserIntent = Intent.createChooser(intent, "分享到:");
			context.startActivity(chooserIntent);
		}
	
	/**
	 * 分享網頁
	 */
	public void shareUrl(String packageName,String className,String content,String title,String subject){
		Intent intent =new Intent();
		intent.setAction(Intent.ACTION_SEND);
		intent.setType("text/plain");
//		if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){
//			
//		}else {			
//			if(null != packageName && !TextUtils.isEmpty(packageName)){
//				intent.setPackage(packageName);
//			}
//		}
		if(stringCheck(className) && stringCheck(packageName)){
			ComponentName componentName = new ComponentName(packageName, className);
			intent.setComponent(componentName);
		}else if(stringCheck(packageName)){
			intent.setPackage(packageName);
		}
		
		intent.putExtra(Intent.EXTRA_TEXT, content);
		if(null != title && !TextUtils.isEmpty(title)){			
			intent.putExtra(Intent.EXTRA_TITLE, title);
		}
		if(null != subject && !TextUtils.isEmpty(subject)){
			intent.putExtra(Intent.EXTRA_SUBJECT, subject);
		}
		intent.putExtra(Intent.EXTRA_TITLE, title);
		Intent chooserIntent = Intent.createChooser(intent, "分享到:");
		context.startActivity(chooserIntent);
	}
	
	/**
	 * 分享圖片
	 */
	public void shareImg(String packageName,String className,File file){
		if(file.exists()){
			Uri uri = Uri.fromFile(file);
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_SEND);
			intent.setType("image/*");
			if(stringCheck(packageName) && stringCheck(className)){
				intent.setComponent(new ComponentName(packageName, className));
			}else if (stringCheck(packageName)) {
				intent.setPackage(packageName);
			}
			intent.putExtra(Intent.EXTRA_STREAM, uri);
			Intent chooserIntent = Intent.createChooser(intent, "分享到:");
			context.startActivity(chooserIntent);
		}else {
			Toast.makeText(context, "檔案不存在", 1000).show();
		}
	}
	
	/**
	 * 分享音樂
	 */
	public void shareAudio(String packageName,String className,File file){
		if(file.exists()){
			Uri uri = Uri.fromFile(file);
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_SEND);
			intent.setType("audio/*");
			if(stringCheck(packageName) && stringCheck(className)){
				intent.setComponent(new ComponentName(packageName, className));
			}else if (stringCheck(packageName)) {
				intent.setPackage(packageName);
			}
			intent.putExtra(Intent.EXTRA_STREAM, uri);
			Intent chooserIntent = Intent.createChooser(intent, "分享到:");
			context.startActivity(chooserIntent);
		}else {
			Toast.makeText(context, "檔案不存在", 1000).show();
		}
	}
	
	/**
	 * 分享視訊
	 */
	public void shareVideo(String packageName,String className,File file){
		setIntent("video/*", packageName, className, file);
	}
	
	public void setIntent(String type,String packageName,String className,File file){
		if(file.exists()){
			Uri uri = Uri.fromFile(file);
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_SEND);
			intent.setType(type);
			if(stringCheck(packageName) && stringCheck(className)){
				intent.setComponent(new ComponentName(packageName, className));
			}else if (stringCheck(packageName)) {
				intent.setPackage(packageName);
			}
			intent.putExtra(Intent.EXTRA_STREAM, uri);
			Intent chooserIntent = Intent.createChooser(intent, "分享到:");
			context.startActivity(chooserIntent);
		}else {
			Toast.makeText(context, "檔案不存在", 1000).show();
		}
	}
	 
	/**
	 * 分享多張圖片和文字至朋友圈
	 * @param title 
	 * @param packageName
	 * @param className
	 * @param file 圖片檔案
	 */
	public void shareImgToWXCircle(String title,String packageName,String className, File file){
		if(file.exists()){
			Uri uri = Uri.fromFile(file);
			Intent intent = new Intent();
			ComponentName comp = new ComponentName(packageName, className);
			intent.setComponent(comp);
			intent.setAction(Intent.ACTION_SEND);
			intent.setType("image/*");
			intent.putExtra(Intent.EXTRA_STREAM, uri);
			intent.putExtra("Kdescription", title);
			context.startActivity(intent);
		}else{
			Toast.makeText(context, "檔案不存在", Toast.LENGTH_LONG).show();
		}
		
		
	}
	/**
	 * 是否安裝分享app
	 * @param packageName
	 */
	public boolean checkInstall(String packageName){
		try {
			context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
			return true;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			Toast.makeText(context, "請先安裝應用app", 1500).show();
			return false;
		}
	}
	
	/**
	 * 跳轉官方安裝網址
	 */
	public void toInstallWebView(String url){
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.setData(Uri.parse(url));
		context.startActivity(intent);
	}
	
	public static boolean stringCheck(String str){
		if(null != str && !TextUtils.isEmpty(str)){
			return true;
		}else {
			return false;
		}
	}
}</span>

二. MainActivity.java類
<span style="font-size:14px;">import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	Button btnQQ;
	Button btnWX;
	Button btnMore;
	Button btnWxFriendText;
	Button btnQQFriendText;
	Button btnWxFriendImg;
	Button btnQQFriendImg;
	Button btnWxFriendAudio;
	Button btnQQFriendAduio;
	Button btnWxFriendVideo;
	Button btnQQFriendVideo;

	ShareUtil shareUtil;
	private Button btn_wxCircle_img;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btnQQ = (Button) findViewById(R.id.btn_qq);
		btnWX = (Button) findViewById(R.id.btn_wx);
		btnMore = (Button) findViewById(R.id.btn_more);
		btnWxFriendText = (Button) findViewById(R.id.btn_wxFriend);
		btnQQFriendText = (Button) findViewById(R.id.btn_qqFriend);
		btnWxFriendImg = (Button) findViewById(R.id.btn_wxFriend_img);
		btnQQFriendImg = (Button) findViewById(R.id.btn_qqFriend_img);
		btnWxFriendAudio = (Button) findViewById(R.id.btn_wxFriend_audio);
		btnQQFriendAduio = (Button) findViewById(R.id.btn_qqFriend_audio);
		btnWxFriendVideo = (Button) findViewById(R.id.btn_wxFriend_video);
		btnQQFriendVideo = (Button) findViewById(R.id.btn_qqFriend_video);
		btn_wxCircle_img = (Button) findViewById(R.id.btn_wxCircle_img);

		btnQQ.setOnClickListener(this);
		btnWX.setOnClickListener(this);
		btnMore.setOnClickListener(this);
		btnWxFriendText.setOnClickListener(this);
		btnQQFriendText.setOnClickListener(this);
		btnWxFriendImg.setOnClickListener(this);
		btnQQFriendImg.setOnClickListener(this);
		btnWxFriendAudio.setOnClickListener(this);
		btnQQFriendAduio.setOnClickListener(this);
		btnWxFriendVideo.setOnClickListener(this);
		btnQQFriendVideo.setOnClickListener(this);
		btn_wxCircle_img.setOnClickListener(this);

		shareUtil = new ShareUtil(this);
	}

	@Override
	public void onClick(View v) {
		String testImgPath = "/storage/emulated/legacy/display-client/picture/my.png";

		String testImagePath = Environment.getExternalStorageDirectory()
				+ "/img.jpg";

		String testAudioPath = Environment.getExternalStorageDirectory()
				+ "/audio.mp3";
		String testVideoPath = Environment.getExternalStorageDirectory()
				+ "/video.mp4";

		File file = new File(testImgPath);
		File fileImage = new File(testImagePath);
		File fileAudio = new File(testAudioPath);
		File fileVideo = new File(testVideoPath);
		switch (v.getId()) {
		// qq&文字
		case R.id.btn_qq:
			shareUtil.shareText("com.tencent.mobileqq", null, "這是一條分享資訊",
					"分享標題", "分享主題");
			break;
		// 微信&文字
		case R.id.btn_wx:
			shareUtil.shareText("com.tencent.mm", null, "這是一條分享資訊", "分享標題",
					"分享主題");
			break;
		// 所有&文字
		case R.id.btn_more:
			shareUtil.shareText(null, null, "這是一條分享資訊", "分享標題", "分享主題");
			break;
		// 微信朋友&文字
		case R.id.btn_wxFriend:
			if (shareUtil.checkInstall("com.tencent.mm")) {
				shareUtil.shareText("com.tencent.mm",
						"com.tencent.mm.ui.tools.ShareImgUI",
						"http://www.aiipu.com/", "分享標題", "分享主題");
			} else {
				shareUtil.toInstallWebView("http://weixin.qq.com/download");
			}
			break;
		// qq朋友&文字
		case R.id.btn_qqFriend:
			if (shareUtil.checkInstall("com.tencent.mobileqq")) {
				shareUtil.shareText("com.tencent.mobileqq",
						"com.tencent.mobileqq.activity.JumpActivity",
						"http://www.aiipu.com/", "分享標題", "分享主題");
			} else {
				shareUtil.toInstallWebView("http://im.qq.com/mobileqq/");
			}
			break;
		// 微信朋友&圖片
		case R.id.btn_wxFriend_img:
			shareUtil.shareImg("com.tencent.mm",
					"com.tencent.mm.ui.tools.ShareImgUI", fileImage);
			break;
		// qq朋友&圖片
		case R.id.btn_qqFriend_img:
			shareUtil.shareImg("com.tencent.mobileqq",
					"com.tencent.mobileqq.activity.JumpActivity", fileImage);
			break;
		case R.id.btn_wxFriend_audio:
			shareUtil.shareAudio("com.tencent.mm",
					"com.tencent.mm.ui.tools.ShareImgUI", fileAudio);
			break;
		case R.id.btn_qqFriend_audio:
			shareUtil.shareAudio("com.tencent.mobileqq",
					"com.tencent.mobileqq.activity.JumpActivity", fileAudio);
			break;
		case R.id.btn_wxFriend_video:
			shareUtil.shareVideo("com.tencent.mm",
					"com.tencent.mm.ui.tools.ShareImgUI", fileVideo);
			break;
		case R.id.btn_qqFriend_video:
			shareUtil.shareVideo("com.tencent.mobileqq",
					"com.tencent.mobileqq.activity.JumpActivity", fileVideo);
			break;
		case R.id.btn_wxCircle_img:
			shareUtil.shareImgToWXCircle("狗狗圖片", "com.tencent.mm",
					"com.tencent.mm.ui.tools.ShareToTimeLineUI", fileImage);
			break;
		}
	}
}</span>


三.佈局檔案activity_main.xml

<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ai.ipu.share_inent.MainActivity" >

	<Button
	    android:id="@+id/btn_qq"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="qq"/>
	
	<Button
	    android:id="@+id/btn_wx"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="wx"
	    android:layout_below="@+id/btn_qq"/>
	
	<Button
	    android:id="@+id/btn_more"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="more"
	    android:layout_below="@+id/btn_wx"/>
	
	<Button
	    android:id="@+id/btn_wxFriend"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/btn_more"
	    android:text="wxFriendText"/>
	
	<Button
	    android:id="@+id/btn_qqFriend"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/btn_wxFriend"
	    android:text="qqFriendText" />
	
	<Button
	    android:id="@+id/btn_wxFriend_img"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/btn_more"
	    android:layout_toRightOf="@+id/btn_wxFriend"
	    android:text="wxFriendImg" />
	
	<Button
	    android:id="@+id/btn_qqFriend_img"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_toRightOf="@+id/btn_qqFriend"
	    android:layout_below="@+id/btn_wxFriend"
	    android:text="qqFriendImg" />
	
	<Button
	    android:id="@+id/btn_wxFriend_audio"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/btn_more"
	    android:layout_toRightOf="@+id/btn_wxFriend_img"
	    android:text="wxFriendAudio" />
	
	<Button
	    android:id="@+id/btn_qqFriend_audio"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_toRightOf="@+id/btn_qqFriend_img"
	    android:layout_below="@+id/btn_wxFriend"
	    android:text="qqFriendAudio" />
	
	<Button
	    android:id="@+id/btn_wxFriend_video"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/btn_qqFriend"
	    android:text="wxFriendVideo" />
	
	<Button
	    android:id="@+id/btn_qqFriend_video"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/btn_wxFriend_video"
	    android:text="qqFriendVideo" />
	
	
	<Button
	    android:id="@+id/btn_wxCircle_img"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/btn_wxFriend_video"
	    android:layout_toRightOf="@+id/btn_wxFriend_img"
	    android:text="wxCircleImg" />
	
	
	
	
</RelativeLayout></span>

其中微信的分享只能分享文字和圖片,不能單獨分享圖片或者文字。