Android AsyncTask, getting SharedPreferences, stuck Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android AsyncTask, getting SharedPreferences, stuck without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m stuck with a problem that seems very easy.. I’ve seen lot of threads concerning my problem, but none of them helped me. It’s about getting Shared Prefferences..

The thing is I want to get result from calculations in doInBackground method, but I just don’t know how to use method getSharedPreferences.

I have my main activity called MainActivity. I also created new Java Class that should do something in the background. Here’s the code:

package com.example.pablo.zad3;

import android.content.SharedPreferences;
import android.os.AsyncTask;

public class Background extends AsyncTask<Integer,Void,String>{

    @Override
    protected String doInBackground(Integer... params) {
        Integer n = params[0];
        StringBuilder sb=new StringBuilder();
       for(int i=1;i<250;i++)
       {
           sb.append(i+ " ");
           sb.append(n*i+"n");
       }
        return sb.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        SharedPreferences prefs = //WHAT NOW??
    }
}

Yet, there’s clearly something I don’t understand, cause I don’t know how to use it. Please provide me with some example code that would work in my case.

Answer

Your code needs to be modified like following:

package com.example.pablo.zad3;

import android.content.SharedPreferences;
import android.content.Context;
import android.os.AsyncTask;

public class Background extends AsyncTask<Integer,Void,String>{

    private Context context;

    public Background(Context context) {
        this.context = context;
    }

    @Override
    protected String doInBackground(Integer... params) {
        Integer n = params[0];
        StringBuilder sb=new StringBuilder();
       for(int i=1;i<250;i++)
       {
           sb.append(i+ " ");
           sb.append(n*i+"n");
       }
        return sb.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
    }
}

Based on the questions you have asked (in main question along with those in comments), I guess you have never used SharedPreferences. Here is the link for your reference. http://developer.android.com/reference/android/content/SharedPreferences.html http://developer.android.com/training/basics/data-storage/shared-preferences.html http://www.pcsalt.com/android/storing-data-in-sharedpreferences-android/

We are here to answer your question about Android AsyncTask, getting SharedPreferences, stuck - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji