很多的應(yīng)用為了應(yīng)用的推廣和傳播都會(huì)使用“分享”的功能,點(diǎn)擊分享按鈕,就能將想要分享的內(nèi)容或者圖片分享至QQ空間、微博、微信朋友圈等實(shí)現(xiàn)了分享功能的應(yīng)用。這篇文章主要是為了學(xué)習(xí)與探索調(diào)用系統(tǒng)實(shí)現(xiàn)分享功能或者直接調(diào)起實(shí)現(xiàn)了分享功能的應(yīng)用的activity來進(jìn)行分享。
目前實(shí)現(xiàn)一鍵分享功能的方式有兩種:
1.需要集成第三方官方SDK包,在獲得官方授權(quán)后調(diào)用其API來完成一鍵分享功能,例如使用友盟分享等
優(yōu)點(diǎn):無縫集成,功能多
缺點(diǎn):需要集成官方的SDK包并通過申請官方的授權(quán)才可進(jìn)行開發(fā)
2.不需要使用任何第三方SDK包,可以直接調(diào)起實(shí)現(xiàn)了分享功能的應(yīng)用的activity來進(jìn)行分享
優(yōu)點(diǎn):不需要使用任何第三方SDK包和申請官方授權(quán)
缺點(diǎn):需要手機(jī)安裝你需要分享的應(yīng)用(這一點(diǎn)非常重要,一開始測試的時(shí)候一直不成功,提示“沒有應(yīng)用可執(zhí)行此操作”,后來找了很久才發(fā)現(xiàn)是我手機(jī)沒有安裝相對應(yīng)的應(yīng)用,這也是不好方便的地方)
按照慣例先來看一下最終效果圖:
ShareActivity.class
package com.xiaolijuan.sharedome;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.RelativeLayout;
import java.io.File;
import java.util.ArrayList;
/**
* 項(xiàng)目名稱:ShareDome
* 類描述:
* 創(chuàng)建人:xiaolijuan
* 創(chuàng)建時(shí)間:2016/1/13 23:48
*/
public class ShareActivity extends Activity implements View.OnClickListener {
private RelativeLayout mRlShareText, mRlShareSingleimage, mRlShareMultipleimage, mRlShareQQ, mRlShareTencent, mRlShareWechat, mRlShareMicroblog, mRlShareOther;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
bindView();
}
private void bindView() {
mRlShareText = (RelativeLayout) findViewById(R.id.rl_share_text);
mRlShareSingleimage = (RelativeLayout) findViewById(R.id.rl_share_singleimage);
mRlShareMultipleimage = (RelativeLayout) findViewById(R.id.rl_share_multipleimage);
mRlShareQQ = (RelativeLayout) findViewById(R.id.rl_share_qq);
mRlShareTencent = (RelativeLayout) findViewById(R.id.rl_share_qqtencent);
mRlShareWechat = (RelativeLayout) findViewById(R.id.rl_share_wechat);
mRlShareMicroblog = (RelativeLayout) findViewById(R.id.rl_share_microblog);
mRlShareOther = (RelativeLayout) findViewById(R.id.rl_share_other);
mRlShareText.setOnClickListener(new ShareText());
mRlShareSingleimage.setOnClickListener(new ShareSingleImage());
mRlShareMultipleimage.setOnClickListener(new ShareMultipleImage());
mRlShareQQ.setOnClickListener(this);
mRlShareTencent.setOnClickListener(this);
mRlShareWechat.setOnClickListener(this);
mRlShareMicroblog.setOnClickListener(this);
mRlShareOther.setOnClickListener(this);
}
//分享文字至所有第三方軟件
public class ShareText implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "這里是分享內(nèi)容");
intent.setType("text/plain");
//設(shè)置分享列表的標(biāo)題,并且每次都顯示分享列表
startActivity(Intent.createChooser(intent, "分享到"));
}
}
//分享單張圖片至所有第三方軟件
public class ShareSingleImage implements View.OnClickListener {
@Override
public void onClick(View v) {
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "13e277bb0b9c2e3ab90229463357bf40.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享到"));
}
}
//分享多張圖片至所有第三方軟件
public class ShareMultipleImage implements View.OnClickListener {
@Override
public void onClick(View v) {
ArrayList<Uri> uriList = new ArrayList<>();
String path = Environment.getExternalStorageDirectory() + File.separator;
uriList.add(Uri.fromFile(new File(path+"13e277bb0b9c2e3ab90229463357bf40.jpg")));
uriList.add(Uri.fromFile(new File(path+"869895e73ddd710e8a132afb37461bf0.jpg")));
uriList.add(Uri.fromFile(new File(path+"4753fc4cd47aa1833c70df4c08f4b7fa.jpg")));
uriList.add(Uri.fromFile(new File(path+"355ee87cf0ff612331a790f31b3ad113.jpg")));
uriList.add(Uri.fromFile(new File(path+"ce61ad4d44e3099d87040f38faabbf56.jpg")));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享到"));
}
}
@Override
public void onClick(View v) {
String pakName = "";
Intent intent = new Intent(Intent.ACTION_SEND); // 啟動(dòng)分享發(fā)送的屬性
intent.setType("text/plain"); // 分享發(fā)送的數(shù)據(jù)類型
switch (v.getId()) {
case R.id.rl_share_qq:
pakName = "com.qzone"; //qq空間
break;
case R.id.rl_share_qqtencent:
pakName = "com.tencent.WBlog"; //騰訊微博
break;
case R.id.rl_share_wechat:
pakName = "com.tencent.mm"; //微信
break;
case R.id.rl_share_microblog:
pakName = "com.sina.weibo"; //微博
break;
case R.id.rl_share_other:
break;
default:
break;
}
intent.setPackage(pakName);
intent.putExtra(Intent.EXTRA_TEXT, "這里是分享內(nèi)容"); // 分享的內(nèi)容
this.startActivity(Intent.createChooser(intent, ""));// 目標(biāo)應(yīng)用選擇對話框的標(biāo)題;
}
}
由于分享功能是使用隱式調(diào)用Activtiy實(shí)現(xiàn)的,則需在響應(yīng)的Activity中聲明intent-filter,在對應(yīng)的activity的xml里加上
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>