很多的應用為了應用的推廣和傳播都會使用“分享”的功能芍耘,點擊分享按鈕开镣,就能將想要分享的內(nèi)容或者圖片分享至QQ空間哗咆、微博、微信朋友圈等實現(xiàn)了分享功能的應用猾担。這篇文章主要是為了學習與探索調用系統(tǒng)實現(xiàn)分享功能或者直接調起實現(xiàn)了分享功能的應用的activity來進行分享袭灯。
目前實現(xiàn)一鍵分享功能的方式有兩種:
1.需要集成第三方官方SDK包,在獲得官方授權后調用其API來完成一鍵分享功能绑嘹,例如使用友盟分享等
優(yōu)點:無縫集成稽荧,功能多
缺點:需要集成官方的SDK包并通過申請官方的授權才可進行開發(fā)
2.不需要使用任何第三方SDK包,可以直接調起實現(xiàn)了分享功能的應用的activity來進行分享
優(yōu)點:不需要使用任何第三方SDK包和申請官方授權
缺點:需要手機安裝你需要分享的應用(這一點非常重要工腋,一開始測試的時候一直不成功姨丈,提示“沒有應用可執(zhí)行此操作”畅卓,后來找了很久才發(fā)現(xiàn)是我手機沒有安裝相對應的應用,這也是不好方便的地方)
按照慣例先來看一下最終效果圖:
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;
/**
* 項目名稱:ShareDome
* 類描述:
* 創(chuàng)建人:xiaolijuan
* 創(chuàng)建時間: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");
//設置分享列表的標題蟋恬,并且每次都顯示分享列表
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); // 啟動分享發(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, ""));// 目標應用選擇對話框的標題;
}
}
由于分享功能是使用隱式調用Activtiy實現(xiàn)的翁潘,則需在響應的Activity中聲明intent-filter,在對應的activity的xml里加上
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>