// APP_ID 替換為你的應(yīng)用從官方網(wǎng)站申請到的合法appIDprivate static final String APP_ID = "你的appid";// IWXAPI 是第三方app和微信通信的openApi接口astatic public IWXAPI api;
在onCreate的方法里面先注冊到微信端 ?
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Workaround in// https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508if (!isTaskRoot()) {// Android launched another instance of the root activity into an existing task// so just quietly finish and go away, dropping the user back into the activity// at the top of the stack (ie: the last state of this task)// Don't need to finish it again since it's finished in super.onCreate .return;}// DO OTHER INITIALIZATION BELOWSDKWrapper.getInstance().init(this);//微信注冊regToWx();}
//注冊到微信private void regToWx() {// 通過WXAPIFactory工廠,獲取IWXAPI的實例api = WXAPIFactory.createWXAPI(this, APP_ID, true);// 將應(yīng)用的appId注冊到微信api.registerApp(APP_ID);//建議動態(tài)監(jiān)聽微信啟動廣播進行注冊到微信registerReceiver(new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {// 將該app注冊到微信//api.registerApp(SyncStateContract.Constants._ID);api.registerApp(APP_ID);}}, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));}