先说下用法:
1、在Tapplication中的wxid(在微信开放平台申请的应用id)与tencentId(在qq开放平台申请的应用id)换成自己的申请的id,将本demo换成自己申请应用的包名。本demo在微信和qq开放平台已经申请好了应用,可以直接使用
2、在清单文件中,AuthActivity里的scheme="xxxx"的数字换成自己的tencentId。
3、popupwiindow是封装好的分享的类,这里面可以写自己想分享的内容(具体想分享什么,可以参考官方文档),以及popupwindow的布局也可以自己设置。
4、在popupwiindow用法,只需在某个avitivity中new popupwiindow(Cotnext,ViewGroup)即可,ViewGroup是指的acitivty的主布局也就是setContentView里的布局
大体说完 直接上demo代码
微信开放平台与qq开放平台提供的sdk在demo的libs里 下载地址
http://download.csdn.net/detail/u012303938/8943141
TappLication.class
public class Tapplication extends Application{
public static Tencent tencent;//QQ
public static IWXAPI iwxapi;//微信
private String tencentId="1104715261";
private String WXID="wx0af4dd6f61b6df32";
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
tencent=Tencent.createInstance(tencentId, this.getApplicationContext());
iwxapi= WXAPIFactory.createWXAPI(this, WXID, true);
iwxapi.registerApp(WXID);
}
PopWindow.class
public class PopWindow implements View.OnClickListener{
PopupWindow popupWindow;
View view;
Context context;
private ImageButton ib_qq,ib_qzone,ib_weiquan,ib_weixin;
public PopWindow(Context context,ViewGroup laytout) {
view =View.inflate(context, R.layout.window,null);
this.context=context;
WindowManager windowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int height=windowManager.getDefaultDisplay().getHeight();
height=height/3;
popupWindow=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, height);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(laytout, Gravity.BOTTOM, 0, 0);
ib_qq=(ImageButton)view.findViewById(R.id.ib_qq);
ib_qq.setOnClickListener(this);
ib_qzone=(ImageButton)view.findViewById(R.id.ib_qzone);
ib_qzone.setOnClickListener(this);
ib_weiquan=(ImageButton)view.findViewById(R.id.ib_weiquan);
ib_weiquan.setOnClickListener(this);
ib_weixin=(ImageButton)view.findViewById(R.id.ib_weixin);
ib_weixin.setOnClickListener(this);
}
public void onDismiss(){
if(popupWindow!=null){
if(popupWindow.isShowing()){
popupWindow.dismiss();
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
//分享到QQ好友
case R.id.ib_qq:
final Bundle bundle = new Bundle();
//这条分享消息被好友点击后的跳转URL。
bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://connect.qq.com/");
//分享的标题。注:PARAM_TITLE、PARAM_IMAGE_URL、PARAM_ SUMMARY不能全为空,最少必须有一个是有值的。
bundle.putString(QQShare.SHARE_TO_QQ_TITLE, "我在测试");
//分享的图片URL
bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,
"http://img3.cache.netease.com/photo/0005/2013-03-07/8PBKS8G400BV0005.jpg");
//分享的消息摘要,最长50个字
bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, "测试");
//手Q客户端顶部,替换“返回”按钮文字,如果为空,用返回代替
bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, "??我在测试");
//标识该消息的来源应用,值为应用名称+AppId。
ThreadManager.getMainHandler().post(new Runnable() {
@Override
public void run() {
Tapplication.tencent.shareToQQ((Activity) context, bundle, new IUiListener() {
@Override
public void onComplete(Object o) {
Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError uiError) {
}
@Override
public void onCancel() {
Toast.makeText(context, "取消分享", Toast.LENGTH_SHORT).show();
}
});
}
});
if(popupWindow.isShowing()){
popupWindow.dismiss();
}
break;
//分享到QQ空间
case R.id.ib_qzone:
final Bundle params = new Bundle();
params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,"http://www.qq.com");
params.putString(QzoneShare.SHARE_TO_QQ_TITLE,"你好");
// params.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL,"http://img3.cache.netease.com/photo/0005/2013-03-07/8PBKS8G400BV0005.jpg");
ArrayList<String> imageUrls = new ArrayList<String>();
imageUrls.add("http://img3.cache.netease.com/photo/0005/2013-03-07/8PBKS8G400BV0005.jpg");
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);
params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "测试分享dasdasdasdadadadasdasda");
params.putString(QzoneShare.SHARE_TO_QQ_APP_NAME, "益卡");
ThreadManager.getMainHandler().post(new Runnable() {
@Override
public void run() {
Tapplication.tencent.shareToQzone((Activity)context,params,qZoneShareListener);
}
});
if(popupWindow.isShowing()){
popupWindow.dismiss();
}
break;
//分享到微信
case R.id.ib_weixin:
WXTextObject textObj = new WXTextObject();
textObj.text="啦啦啦啦德玛西亚";
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
msg.description = "详细情况大舒服撒的方法";
SendMessageToWX.Req req=new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis()); // transaction字段用于唯一标识一个请求
req.message = msg;
req.scene=SendMessageToWX.Req.WXSceneSession;
Tapplication.iwxapi.sendReq(req);
if(popupWindow.isShowing()){
popupWindow.dismiss();
}
//分享到朋友圈
case R.id.ib_weiquan:
WXTextObject textObj1 = new WXTextObject();
textObj1.text="啦啦啦啦德玛西亚";
WXMediaMessage msg1 = new WXMediaMessage();
msg1.mediaObject = textObj1;
msg1.description = "详细情况大舒服撒的方法";
SendMessageToWX.Req req1=new SendMessageToWX.Req();
req1.transaction = String.valueOf(System.currentTimeMillis()); // transaction字段用于唯一标识一个请求
req1.message = msg1;
req1.scene=SendMessageToWX.Req.WXSceneTimeline;
Tapplication.iwxapi.sendReq(req1);
if(popupWindow.isShowing()){
popupWindow.dismiss();
}
break;
}
}
IUiListener qZoneShareListener = new IUiListener() {
@Override
public void onCancel() {
Toast.makeText(context,"取消分享",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError e) {
// TODO Auto-generated method stub
Toast.makeText(context,e.errorMessage,Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(Object response) {
// TODO Auto-generated method stub
Toast.makeText(context,"分享成功",Toast.LENGTH_SHORT).show();
}
};
}
MainActivity.class去调用
public class MainActivity extends ActionBarActivity {
private Button btn_qq;
private RelativeLayout rl_pop;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
btn_qq=(Button) findViewById(R.id.btn_qq);
rl_pop=(RelativeLayout) findViewById(R.id.rl_pop);
btn_qq.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new PopWindow(context, rl_pop);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}