Reputation: 98
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.
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)
Upvotes: 0
Views: 193
Reputation: 3017
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). Provided the MobileServiceClient.getCurrentUser() method returns a valid user, you can just make calls to in your background service. If you don't have a valid user at that point, then you either have to show the dialog on the UI thread (i.e. NOT in your background service) or you can make unauthenticated calls to your Mobile Service. 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
}
Upvotes: 1