ShareSDK三方分享

1.下载最新的shareSDK解压到文件夹下


2.配置分享的详情



/**
 * 执行分享的线程
 * 
 * @author 王永迪 2015.3.13
 */
public class ShowShareTask extends Task implements PlatformActionListener,
Callback {
private Activity context;
private TaskHandler hanlder;
private String screenPath;
public static final int MSG_TOAST = 1;
public static final int MSG_ACTION_CCALLBACK = 2;
public static final int MSG_CANCEL_NOTIFY = 3;


public static final int Creation_Share = 1;
public static final int Activity_Share = 2;
public int SHARE_TYPE = 1;


public ShowShareTask(Activity act, TaskHandler hanlder, String screenPath,
int type) {
super(act, hanlder);
// TODO Auto-generated constructor stub
this.context = act;
this.hanlder = hanlder;
this.screenPath = screenPath;
this.SHARE_TYPE = type;
}


@Override
public void run() {
// TODO Auto-generated method stub
OnekeyShare oks = new OnekeyShare();
// 设置Notification的显示图标和显示文字
oks.setNotification(R.drawable.ic_launcher, "3D 魔法相机");
switch (SHARE_TYPE) {
case Creation_Share:
// 分享内容的标题
oks.setTitle("【3D魔法相机】专为自拍症患者定制的APP,药可以停,自拍不能停.");
// oks.setTitleUrl("http://a.app.qq.com/o/simple.jsp?pkgname=com.haier.dollprint");
// 微信和易信的分享的网络连接,如果没有可以不设置
// oks.setUrl("http://a.app.qq.com/o/simple.jsp?pkgname=com.haier.dollprint");
oks.setText("人类进化史上“醉”吊APP,只为专注自拍的你--来自【3D魔法相机】");
// 调用shareSDK自己的分享截图
// View view = context.getWindow().getDecorView();
// // oks.setViewToShare(view);
oks.setBitmapToShare(combinateFrame(Img2Base64
.readBitmap(screenPath)));
break;
case Activity_Share:
// 分享内容的标题
oks.setTitle("【3D魔法相机】专为自拍症患者定制的APP,药可以停,自拍不能停.");
oks.setTitleUrl("http://a.app.qq.com/o/simple.jsp?pkgname=com.haier.dollprint");
// 微信和易信的分享的网络连接,如果没有可以不设置
// oks.setUrl("http://a.app.qq.com/o/simple.jsp?pkgname=com.haier.dollprint");
oks.setText("人类进化史上“醉”吊APP,只为专注自拍的你--来自【3D魔法相机】");
// 调用shareSDK自己的分享截图
// View view = context.getWindow().getDecorView();
// // oks.setViewToShare(view);
oks.setBitmapToShare(combinateFrame(Img2Base64
.readBitmap(screenPath)));
// 设置分享照片的本地路径,如果没有可以不设置
// 调用glview的分享截图
// oks.setImagePath(msg.obj.toString());
// 设置分享的主题样式
// oks.setTheme(OnekeyShareTheme.SKYBLUE);
break;
}
// 程序的名称或者是站点名称
oks.setSite("3D 魔法相机");
// 程序的名称或者是站点名称的链接地址
oks.setSiteUrl("http://a.app.qq.com/o/simple.jsp?pkgname=com.haier.dollprint");
// 设置是否是直接分享
oks.setSilent(false);
// 如果是英文环境下 隐藏国内的分享平台
if (Common.getCountry(context) == CommonVariable.English) {
oks.addHiddenPlatform(SinaWeibo.NAME);
oks.addHiddenPlatform(TencentWeibo.NAME);
oks.addHiddenPlatform(QQ.NAME);
oks.addHiddenPlatform(Wechat.NAME);
oks.addHiddenPlatform(WechatMoments.NAME);
oks.addHiddenPlatform(QZone.NAME);
// 如果是英文环境下 隐藏国外的分享平台
} else {
oks.addHiddenPlatform(Twitter.NAME);
oks.addHiddenPlatform(Facebook.NAME);
}
// 显示
oks.show(context);
}


/**
* 图片与边框组合

* @param bm
*            原图片
* @param res
*            边框资源
* @return
*/
private Bitmap combinateFrame(Bitmap bm) {
// 原图片的宽高
float radio = (float) bm.getWidth() / (float) bm.getHeight();
float small = Math.abs(9.f / 16.f - radio);
float max = Math.abs(10.f / 16.f - radio);
int resid = R.drawable.photo3;
int bitmapheight = 0;
int bitmapwidth = 0;


if (Math.min(small, max) == small) {
// 9/16的图
if (CommonVariable.sBodyType == CommonVariable.MAN)// 男
{
resid = R.drawable.photo1;


} else// 女
{
resid = R.drawable.photo2;
}
bitmapheight = 1920;
bitmapwidth = 1080;
} else {
// 10/16的图
if (CommonVariable.sBodyType == CommonVariable.MAN
|| CommonVariable.sBodyType == CommonVariable.BOY)// 男
{
resid = R.drawable.photo3;
} else// 女
{
resid = R.drawable.photo4;
}
bitmapheight = 1728;
bitmapwidth = 1080;
}


int bigH = bm.getHeight();
int bigW = bitmapwidth * bigH / bitmapheight;


// 重新定义大小
Bitmap newBitmap = Utils.createBitmap(bigW, bigH, Config.ARGB_8888);


Canvas canvas = new Canvas(newBitmap);


if (bigW > bm.getWidth()) {
// 绘原图
canvas.drawBitmap(
bm,
new Rect(0, 0, bm.getWidth(), bigH),
new Rect((bigW - bm.getWidth()) / 2, 0, bigW
- (bigW - bm.getWidth()) / 2, bigH), null);
} else {
canvas.drawBitmap(
bm,
new Rect((bm.getWidth() - bigW) / 2, 0, bm.getWidth()
- (bm.getWidth() - bigW) / 2, bigH), new Rect(0, 0,
bigW, bigH), null);
}
bm.recycle();
bm = null;
// Bitmap bitmap =
// ((BitmapDrawable)getResources().getDrawable(resid)).getBitmap();
BitmapFactory.Options bo = new BitmapFactory.Options();
bo.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
resid, bo);


Matrix matrix = new Matrix();
matrix.setScale((float) bigW / (float) bitmap.getWidth(), (float) bigH
/ (float) bitmap.getHeight());
canvas.drawBitmap(bitmap, matrix, null);
bitmap.recycle();
bitmap = null;
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return newBitmap;
}


@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case MSG_TOAST: {
String text = String.valueOf(msg.obj);
hanlder.sendObjectMessage(Task.CUSTOM, text, MSG_TOAST);
}
break;
case MSG_ACTION_CCALLBACK: {
switch (msg.arg1) {
case 1: // 成功后发送Notification
showNotification(2000, "分享完成");
break;
case 2: // 失败后发送Notification
showNotification(2000, "分享失败");
break;
case 3: // 取消
showNotification(2000, "取消分享");
break;
}
}
break;
case MSG_CANCEL_NOTIFY:
NotificationManager nm = (NotificationManager) msg.obj;
if (nm != null) {
nm.cancel(msg.arg1);
}
break;
}
return false;
}


// 根据传入的参数显示一个Notification
@SuppressWarnings("deprecation")
private void showNotification(long cancelTime, String text) {
try {
Context app = context;
NotificationManager nm = (NotificationManager) app
.getSystemService(Context.NOTIFICATION_SERVICE);
final int id = Integer.MAX_VALUE / 13 + 1;
nm.cancel(id);
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.setting,
text, when);
PendingIntent pi = PendingIntent.getActivity(app, 0, new Intent(),
0);
notification.setLatestEventInfo(app, "sharesdk test", text, pi);
notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(id, notification);


if (cancelTime > 0) {
Message msg = new Message();
msg.what = MSG_CANCEL_NOTIFY;
msg.obj = nm;
msg.arg1 = id;
UIHandler.sendMessageDelayed(msg, cancelTime, this);
}
} catch (Exception e) {
e.printStackTrace();
}
}


// 取消后的回调方法
@Override
public void onCancel(Platform platform, int action) {
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 3;
msg.arg2 = action;
msg.obj = platform;
UIHandler.sendMessage(msg, this);
}


// 完成后的回调方法
@Override
public void onComplete(Platform platform, int action,
HashMap<String, Object> arg2) {
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 1;
msg.arg2 = action;
msg.obj = platform;
UIHandler.sendMessage(msg, this);
}


// 出错后的回调方法
@Override
public void onError(Platform platform, int action, Throwable t) {
t.printStackTrace();
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 2;
msg.arg2 = action;
msg.obj = t;
UIHandler.sendMessage(msg, this);
}
}

3.导入JAR包


4.配置 xml文件

<!-- 微信分享回调 -->
        <activity
            android:name="com.example.thirddemo2.wxapi.WXEntryActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

以及一些常用的权限加上去就可以了。。。

你可能感兴趣的:(ShareSDK三方分享)