问题描述
- 微信sdk朋友圈分享,到不了分享界面
-
package com.example.shareweixin;import com.example.shareweixin.R;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;public class MainActivity extends Activity {
public static final String APP_ID="wx2fb682b1dce9d873";
private IWXAPI api;
CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
api=WXAPIFactory.createWXAPI(this,APP_ID);
//将APP_ID注册到微信中
api.registerApp(APP_ID);
checkBox=(CheckBox) findViewById(R.id.checkBox_share_friends);} @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; } //启动微信客户端
public void onclick(View view) {
api.openWXApp();
}
private String Transcation(String type) {return (type==null)?String.valueOf(System.currentTimeMillis()):type+System.currentTimeMillis();
}
public void send_text(View view) {
final EditText editText=new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
editText.setText("默认的文本");
final AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setTitle("共享文本");
builder.setView(editText);
builder.setMessage("请输入要分享的文本");
builder.setPositiveButton("分享", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface arg0, int arg1) { //获取分享的文本 String text=editText.getText().toString(); if (text==null||text.length()==0) { return; } //1、创建一个textObjiec对象拥有分装待分享文本; WXTextObject textObject=new WXTextObject(); textObject.text=text; //2、创建一个mediamessage用于传输text; WXMediaMessage msg=new WXMediaMessage(); msg.mediaObject=textObject; msg.description=text; //3、创建一个向微信发送请求的SendMessageToWX.Req(); SendMessageToWX.Req Req=new SendMessageToWX.Req(); Req.message=msg; //设置请求的唯一标识符 Req.transaction=Transcation("text"); //表示发送给朋友圈还是朋友 Req.scene=checkBox.isChecked()?SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession; //4、发送给微信客户端 ; Toast.makeText(MainActivity.this, String.valueOf(api.sendReq(Req)), Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", null); AlertDialog alertDialog=builder.create(); alertDialog.show();
}
}点击layput的xml文件里面的打开微信的按钮可以,但是在弹出的对话框分享跳不到微信朋友圈界面
解决方案
微信及朋友圈分享
友盟分享到微信,朋友圈
分享微信及朋友圈(基于友盟)
解决方案二:
你的APP ID填写正确吗 配置文件里面该添加的添加没得