试试这个代码:
public class DownloadFiles extends AsyncTask<URL, Void, Void> {
Context ctx;
boolean firstLaunch;
SharedPreferences prefs;
DownloadFiles(Context ctx) {
this.ctx = ctx;
prefs = ctx.getSharedPreferences("Prefs",Context.MODE_PRIVATE);
}
@Override
public void onPreExecute() {
firstLaunch = prefs.getBoolean("firstrun", true);
}
@Override
public void doInBackground(URL... urls) {
if(firstLaunch)
// code for the firstLaunch
else
//code if it isn't the firstLaunch
}
@Override
public void onPostExecute(Void params) {
// update prefs after the firstLaunch
if(firstLaunch)
prefs.edit().putBoolean("firstrun", false).commit();
}
}