Android调用微信登陆跳转时没有确认登陆,但正常返回code,请问怎么解决?
发送普通字符串时能正常跳转到微信,但调用登陆授权时没有确认登陆就返回了code,然后调用获取access_token也是正常返回,请问哪里不对了?
我的代码:
public class MainActivity extends AppCompatActivity {
String APP_ID ="xxx";
public static IWXAPI api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] permissionsReq = new String[] { Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permissionsReq, 1);
}
api = WXAPIFactory.createWXAPI(this, APP_ID, true);
api.registerApp(APP_ID);
}
public void onClick(View view){
WXTextObject textObj = new WXTextObject();
textObj.text = "123";
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
msg.description = "123";
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
api.sendReq(req);
}
public void test2(View view){
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "wechat_sdk_demo_test222";
boolean result = api.sendReq(req);
Toast.makeText(this, ""+ result, Toast.LENGTH_SHORT).show();
}
}
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity.api.handleIntent(getIntent(), this);
}
// @Override
// protected void onNewIntent(Intent intent){
// super.onNewIntent(intent);
// MainActivity.api.handleIntent(getIntent(),this);
// Log.e("ansen","WXEntryActivity onNewIntent");
// }
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp baseResp) {
Log.e("onResp","errCode: " + baseResp.errCode);
if(baseResp instanceof SendAuth.Resp){
SendAuth.Resp newResp = (SendAuth.Resp) baseResp;
//获取微信传回的code
String code = newResp.code;
Log.e("onResp","code: " + code);
Log.e("onResp","state: " + newResp.state);
}
}
}
manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bxt.com.bxt">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name="com.example.admin.test_wechat.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.example.admin.test_wechat.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".wxapi.WXEntryActivity"
android:exported="true"/>
</application>
</manifest>
app build.gradle
android {
compileSdkVersion 27
defaultConfig {
// applicationId "com.example.admin.test_wechat"
applicationId "bxt.com.bxt"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
keyAlias 'bxt'
keyPassword '12345678'
storeFile file('release_keystore.jks')
storePassword '12345678'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.release
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论