永远不会调用onNewIntent

编程入门 行业动态 更新时间:2024-10-20 01:25:13
本文介绍了永远不会调用onNewIntent-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用AppAuth创建一个简单的oauth2流.我正在关注AppAuth的本教程.它可以很好地完成oauth请求,但是在授权后,当涉及到主要活动时,它就永远不会调用onNewIntent,我还检查了讨论过的问题.

I am trying to create a simple oauth2 flow using AppAuth. I am following this tutorial of AppAuth. It is doing good up to making an oauth request but after authorization when it comes to main activity then it never calls onNewIntent, I also checked the question discussed here.

:当我使用onResume方法时,经过授权但使用"android.intent.action.MAIN"动作涉及到onResume方法.应该对onResume进行"com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE"操作.

When I use onResume method then it comes to onResume method after authorization but with "android.intent.action.MAIN" action. Where it should come with "com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE" action on onResume.

有什么建议为什么会发生吗?

Any suggestion why it is happening?

以下是MainActivity类

following is the MainActivity class

public class MainActivity extends AppCompatActivity { private static final String USED_INTENT = "USED_INTENT"; public static final String LOG_TAG = "AppAuthSample"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void setupAuthorization(View view) { AuthorizationServiceConfiguration serviceConfiguration = new AuthorizationServiceConfiguration( Uri.parse("accounts.google/o/oauth2/v2/auth"), //* auth endpoint *//*, Uri.parse("oauth2.googleapis/token") //* token endpoint *//* ); String clientId = "MY_ID.apps.googleusercontent"; Uri redirectUri = Uri.parse("com.demo.testdriveapi:/oauth2callback"); AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder( serviceConfiguration, clientId, "code", redirectUri ); builder.setScopes("www.googleapis/auth/drive.appdata"); AuthorizationRequest request = builder.build(); AuthorizationService authorizationService = new AuthorizationService(this); String action = "com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE"; Intent postAuthorizationIntent = new Intent(action); PendingIntent pendingIntent = PendingIntent.getActivity(this, request.hashCode(), postAuthorizationIntent, 0); authorizationService.performAuthorizationRequest(request, pendingIntent); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); checkIntent(intent); } private void checkIntent(@Nullable Intent intent) { if (intent != null) { String action = intent.getAction(); switch (action) { case "com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE": if (!intent.hasExtra(USED_INTENT)) { handleAuthorizationResponse(intent); intent.putExtra(USED_INTENT, true); } break; default: // do nothing } } } @Override protected void onStart() { super.onStart(); checkIntent(getIntent()); } private void handleAuthorizationResponse(@NonNull Intent intent) { AuthorizationResponse response = AuthorizationResponse.fromIntent(intent); AuthorizationException error = AuthorizationException.fromIntent(intent); final AuthState authState = new AuthState(response, error); if (response != null) { Log.i(LOG_TAG, String.format("Handled Authorization Response %s ", authState.jsonSerializeString())); AuthorizationService service = new AuthorizationService(this); service.performTokenRequest(response.createTokenExchangeRequest(), new AuthorizationService.TokenResponseCallback() { @Override public void onTokenRequestCompleted(@Nullable TokenResponse tokenResponse, @Nullable AuthorizationException exception) { if (exception != null) { Log.w(LOG_TAG, "Token Exchange failed", exception); } else { if (tokenResponse != null) { authState.update(tokenResponse, exception); Log.i(LOG_TAG, String.format("Token Response [ Access Token: %s, ID Token: %s ]", tokenResponse.accessToken, tokenResponse.idToken)); } } } }); } } }

这是AndroidMenifest.xml中的代码段

Here is the snippet from AndroidMenifest.xml

<application 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=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name="net.openid.appauth.RedirectUriReceiverActivity"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="com.demo.testdriveapi"/> </intent-filter> </activity> </application>

这是activity_main.xml的代码段,我只有一个按钮

and here is the snippet of activity_main.xml, I only have one button

<Button android:id="@+id/buttonAuthorize" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="153dp" android:layout_marginTop="288dp" android:layout_marginEnd="170dp" android:onClick="setupAuthorization" android:text="Authorize" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />

也尝试使用"android:launchMode="singleTop",但它也不起作用.

Also tried using "android:launchMode="singleTop" but it also doesn't work.

Edit2 :添加Google开发者控制台的屏幕截图

Adding screenshot of Google developer console

推荐答案

如果我正确理解您的信息,则说明您已经成功配置了AppAuth,并且在启动身份验证机制时,用户可以输入其用户名和密码来登录openId或不管是什么.但是这样做时,您无法拦截该意图的结果(chrome自定义标签).

If I understand you correctly, you have successfully configured AppAuth and on launching the authentication mechanism, the user is able to enter their username and password to login into openId or whatever it is. But on doing so you cannot intercept the result of that intent (chrome custom tabs).

如果是这样,请尝试使用此覆盖方法

If so try this override method

onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)

您还需要startActivityForResult(authIntent, REQUEST_CODE)

更多推荐

永远不会调用onNewIntent

本文发布于:2023-11-26 20:54:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635155.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   onNewIntent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!