I'm trying to convert the things executed in my service to AsyncTask to make it better. However, I'm new to this concept and I've been having trouble setting it up (not knowing what to put where). This service is executed on onHandleIntent
once a button from MainActivity
is pressed.
the part that should run in doInBackground() (collectData() gets the data usage info about apps in the phone):
Utils.d("On Handle Inent Called -- Will collect Data");
newStartTime = intent.getLongExtra("startTime",0);
newEndTime = intent.getLongExtra("endTime",0);
dataUsage = collectData();
the part that runs onPostExecute() (starts a new activity that lists the apps and their data usage):
isWifi = intent.getBooleanExtra("isWifi",true);
Intent intentActivity = new Intent(this, DataDisplayActivity.class);
intentActivity.putExtra("dataUsage",dataUsage.toString());
intentActivity.putExtra("isWifi",isWifi);
startActivity(intentActivity);
Can anyone help?
new AsyncTask<Void, Void, Void>() {
@Override
protected Void> doInBackground(Void... params) {
Utils.d("On Handle Inent Called -- Will collect Data");
newStartTime = intent.getLongExtra("startTime",0);
newEndTime = intent.getLongExtra("endTime",0);
dataUsage = collectData();
return null;
}
@Override
protected void onPostExecute(Void void) {
super.onPostExecute(showsForBucket);
isWifi = intent.getBooleanExtra("isWifi",true);
Intent intentActivity = new Intent(this, DataDisplayActivity.class);
intentActivity.putExtra("dataUsage",dataUsage.toString());
intentActivity.putExtra("isWifi",isWifi);
startActivity(intentActivity);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.