Android调用微信登陆跳转时没有确认登陆,但正常返回code,请问怎么解决?

发布于 2022-09-07 07:26:31 字数 5029 浏览 24 评论 0

发送普通字符串时能正常跳转到微信,但调用登陆授权时没有确认登陆就返回了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文