First, I have to say I'm kinda new to native Android development. I put the following code inside onCreate in my MainActivity
class.
I never used AsyncTask, so I don't know if this is the best practice to do it. Should I create a new AsyncTask-class and load it somehow in the onCreate method? I put it in the MainActivity onCreate method, because of the scope of variables like bp
(billing purchase class), pollfish, counting app-launches, etc.
Also I don't know what to return? Because I have multiple results. There are many tutorials how to create an AsyncTask like this and also suggestions to store SharedPreferences there, but I want to know the best practice for it. For example to get values later in my onResume method or similiar operations.
new AsyncTask<Context, Void, String>()
{
@Override
protected String doInBackground(Context... params)
{
Context context = params[0];
SharedPreferences pref = context.getSharedPreferences("Pref",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
// Count App Launches
totalCount = pref.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.apply();
// Pollfish ADS
int pollfishPref = pref.getInt("pollfish", 0);
Log.d(TAG,"Application launch count: " + totalCount);
Log.d(TAG,"pollfish count: " + pollfishPref);
if (!bp.isPurchased(PRODUCT_ID) && (pollfishPref == 0 || (pollfishPref != 0 && pollfishPref <= totalCount)))
{
PollFish.ParamsBuilder paramsBuilder = new PollFish.ParamsBuilder("xxx")
.releaseMode(false)
.customMode(false)
.indicatorPosition(Position.MIDDLE_RIGHT)
.indicatorPadding(0)
.build();
PollFish.initWith(MainActivity.this, paramsBuilder);
mTracker.setScreenName("Pollfish Count: " + pollfishPref + " Applaunch Count: " + totalCount);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}
Boolean consumed = pref.getBoolean("consumed", Boolean.parseBoolean(null));
Log.d(TAG,"consumedPref: " + consumed);
if (!consumed)
{
editor.putBoolean("consumed", true);
editor.apply();
bp.consumePurchase(PRODUCT_ID);
Log.d(TAG,"consumed now!");
}
return xxx;
}
protected void onPostExecute(String result)
{
Log.d(TAG, "Preference received in background: " + result);
};
}.execute(this);