Reputation: 684
I have a background service that is responsible for checking for updates from a server.
The services launches an AsyncTask, which is responsible for the actual check and download of update.
Then I launch an intent to request the user to install or cancel the installation
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(newFile(SystemConstants.UPDATE_DOWNLOAD_PATH+fileName)),"application/vnd.android.packagearchive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
In an Activity
I would startActivityForResult
but since a service
has started the request I'm having a hard time figuring out how to wait for user response.
How can I, within the AsyncTask wait until the user has chosen an action?
Thanks
Upvotes: 0
Views: 136
Reputation: 75629
I'm having a hard time figuring out how to wait for user response
If you need a feedback then your service should not directly fire this intent. Instead you should have "middle man" in your app, most likely another activity, which will fire that intent instead, once service tells it do that. And since this will be regular activity, you can use startActivityForResult()
Yet, I think that you should read about event bus concept (GreenRobot's, OTTO, Guava's) as this may be helpful for your in-app communication.
Upvotes: 1
Reputation: 12899
First of all, starting a download in background seems like a bad practice to me unless the size of it is very small. The user should be made aware there is an upgrade and asked if it's ok to proceed.
That aside I think you should use Notification
and PendingIntent
. When the user click your notification your pending intent will just open an Activity
that launch your startActivityForResult()
.
If this check is a periodic task make sure you use a JobScheduler
to optimize battery usage and avoid your service getting terminated by the OS.
Upvotes: 1