I have three separate applications A, B and C. All of them use service S. When A, B or C connect to service S and service S must start, it must gather credentials from the user using form F.
I want the user experience to go like this:
- From the Android main menu, the user taps one of the applications; A, B or C.
- Form F appears because the service has not been started.
- The user fills in form F and taps OK.
- Validation occurs.
- If validation of credential开发者_高级运维s is successful, then the appropriate Activity in the selected app (A, B or C) appears.
- After a successful validation, any subsequent applications that start do not need to authenticate, they'll just start with an Activity in that app.
- If the validation is unsuccessful and the user presses the back button, the main menu appears again.
Given all of this, what I believe I want is for the Service to start the credentials form when necessary -- can a Service do this? If not, what would be an alternative way to implement this?
A Service is a Context, and a Context can start an Activity:
https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)
A Service should never start an Activity directly. This is clearly stated in the documentation on Notifications. You can view it at this link (please refer to the second paragraph).
The scenario that you described can be handled in an alternate way:
- Add a method to the service
isValidated()
which returns true if the user has already been validated. - Activity A, B or C should
use
isValidated()
to first check whether the user is validated or not. If not the Activity should show the from F.
精彩评论