简体   繁体   English

如何在Android服务中对Azure移动服务进行身份验证

[英]How to authenticate Azure Mobile Services in Android Service

I followed the instructions for authenticating in an Activity and that worked fine. 我遵循了在活动中进行身份验证的说明,并且工作正常。

But I want to authenticate in a service. 但是我想在服务中进行身份验证。 That doesn't work because it requires to display a WebView in a dialog, even though I'm already authenticated and the dialog will just disappear. 那是行不通的,因为即使我已经通过身份验证并且对话框将消失,它也需要在对话框中显示WebView。

Here's the stack trace, with my Service replaced with *** 这是堆栈跟踪,我的服务已替换为***

08-03 18:19:56.824  12259-12259/org.l6n.hn E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: org.l6n.hn, PID: 12259
    java.lang.RuntimeException: Unable to create service ***: java.lang.ClassCastException: *** cannot be cast to android.app.Activity
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2595)
            at android.app.ActivityThread.access$1800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: *** cannot be cast to android.app.Activity
            at com.microsoft.windowsazure.mobileservices.LoginManager.showLoginUI(LoginManager.java:282)
            at com.microsoft.windowsazure.mobileservices.LoginManager.authenticate(LoginManager.java:143)
            at com.microsoft.windowsazure.mobileservices.MobileServiceClient.login(MobileServiceClient.java:230)
            at com.microsoft.windowsazure.mobileservices.MobileServiceClient.login(MobileServiceClient.java:214)
            at ***.authenticate(***.java:118)
            at ***.onCreate(***.java:47)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2585)
            at android.app.ActivityThread.access$1800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Provided your user has already authenticated, you don't need to call the login method again (which is what is trying to show the dialog). 如果您的用户已经通过身份验证,则无需再次调用login方法(这就是试图显示对话框的方法)。 Provided the MobileServiceClient.getCurrentUser() method returns a valid user, you can just make calls to in your background service. 只要MobileServiceClient.getCurrentUser()方法返回有效用户,您就可以在后台服务中进行调用。 If you don't have a valid user at that point, then you either have to show the dialog on the UI thread (ie NOT in your background service) or you can make unauthenticated calls to your Mobile Service. 如果此时您没有有效的用户,则必须在UI线程上显示对话框(即不在后台服务中),或者可以对移动服务进行未经身份验证的调用。 So in hour background service you'd probably want something like this: 因此,在小时后台服务中,您可能想要这样的东西:

if (myMobileServiceClient.getCurrentUser() != null && myMobileServiceClient.getCurrentUser().getUserId() != "") {
    //do your background service calls to your Mobile Service
} else {
    //Fire something back to the UI thread to prompt a login and restart service
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM