Android應用中對於微信分享的實例及問題
如何分享
分享無相應
分享結果如何接收響應
微信 分享回調
(提示幾點關鍵問題: debug_key 一定要獲得對應的簽名碼 然後和weixin官網的appid對應 )
幾點注意事項
1)必須新建一個包位置是
<you package>.wxapi
2)並在此包下簡曆class
WXEntryActivity.class
3)AndroidManifest.xml 需要添加對應Activity的聲明
<activity
android:name="com.yinhang.pos.wxapi.WXEntryActivity"
android:exported="true"
android:theme="@android:style/Theme.Translucent" >
4) WXEntryActivity.class 的基本要求
public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler{
RelativeLayout
<RelativeLayout xmlns:andro xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".StartActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="share" /> </RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="com.example.weishare" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.weishare.activity.StartActivity" android:exported="true" android:launchMode="singleTop" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.weishare.wxapi.WXEntryActivity" android:exported="true" android:theme="@android:style/Theme.Translucent" > </activity> </application> </manifest>
WXEntryActivity 響應 發送返回的關鍵
public class WXEntryActivity extends Activity implements IWXAPIEventHandler { private IWXAPI api; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); api = WXAPIFactory.createWXAPI(this, "your app id", false); api.handleIntent(getIntent(), this); } @Override public void onReq(BaseReq arg0) { } @Override public void onResp(BaseResp resp) { int result = 0; switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: result = R.string.errcode_success; break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = R.string.errcode_cancel; break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = R.string.errcode_deny; break; default: result = R.string.errcode_unknown; break; } Toast.makeText(this, result, Toast.LENGTH_LONG).show(); // TODO 微信分享 成功之後調用接口 this.finish(); } }
StartActivity
public class StartActivity extends Activity { private static final String APP_ID = "your app id"; private IWXAPI api; private static final String text = "sdafljsalfdkj"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); findViewById(R.id.btn_share).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub final EditText editor = new EditText(StartActivity.this); editor.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); editor.setText("默認發送信息"); // MMAlert.showAlert(StartActivity.class, "title", editor, "ok", "cancel", new DialogInterface.OnClickListener()) regToWx(); // 初始化一個WXTextObject對象 WXTextObject textObj = new WXTextObject(); textObj.text = text; // 用WXTextObject對象初始化一個WXMediaMessage對象 WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; // 發送文本類型的消息時,title字段不起作用 // msg.title = "Will be ignored"; msg.description = text; // 構造一個Req SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = String.valueOf(System.currentTimeMillis());//buildTransaction("text"); // transaction字段用於唯一標識一個請求 req.message = msg; // req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; Log.e("",api.sendReq(req) + "----------------"); // 調用api接口發送數據到微信 api.sendReq(req); } }); } private void regToWx(){ api = WXAPIFactory.createWXAPI(this, APP_ID, true); api.registerApp(APP_ID); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); // Toast.makeText(this, "onRestart sdkjflsaj", Toast.LENGTH_LONG).show(); Log.e(this.getApplication().toString(), "---------onRestart-----------------------"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); // Toast.makeText(this, "onResume lasdkjflsaj", Toast.LENGTH_LONG).show(); Log.e(this.getApplication().toString(), "---------onResume-----------------------"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.start, menu); return true; } // @Override // public void onReq(BaseReq arg0) { // // TODO Auto-generated method stub //// Toast.makeText(this, "onReqonReqonReqlasdkjflsaj", Toast.LENGTH_LONG).show(); // Log.e(this.getApplication().toString(), "---------onReq-----------------------"); // } // // @Override // public void onResp(BaseResp arg0) { // // TODO Auto-generated method stub //// Toast.makeText(this, "onResponResponRespasdkjflsaj", Toast.LENGTH_LONG).show();; // Log.e(this.getApplication().toString(), "---------onResp-----------------------"); // // // } }
最後更新:2017-04-03 12:55:04