Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9163541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:28:19+00:00 2026-06-17T14:28:19+00:00

I am trying to get Shared Preferences in an AsyncTask, but I don’t get

  • 0

I am trying to get Shared Preferences in an AsyncTask, but I don’t get it.

How do I have to use context here?

package com.example.wettkampftimerbt;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;

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

public class network extends AsyncTask<URL, String, String> {

    private final String PREFS_NAME = "prefs", W1 = "w1",  W2="w2", W3="w3", W4="w4", 
            W5="w5", W6="w6", W7="w7", SENDBEG="sendbeg", SENDEND="sendend";
    int w1, w2, w3, w4, w5, w6, w7, sendend, sendbeg;

    protected String doInBackground(URL... params) {
        int c2, c3, c4, c5, c6;
        int w0 = 00;
        try {
            w1 = sendbeg;
            c2 = w2;
            c3 = w3;
            c4 = w4;
            c5 = w5;
            c6 = w6;
            w7 = sendend;

            Socket ss = new Socket("192.168.3.100", 4455);
            System.out.println(ss);
            boolean stopData = true;
            System.out.println("lane1 stop send");
            DataOutputStream dos = new DataOutputStream(
                    ss.getOutputStream());
            while (stopData) {
                dos.writeByte(w0);
                dos.writeByte(w1);
                dos.writeByte(c2);
                dos.writeByte(c3);
                dos.writeByte(c4);
                dos.writeByte(c5);
                dos.writeByte(c6);
                dos.writeByte(w7);
                stopData=false;
            }
        } catch (IOException e) {
            System.out.println("IO error" + e);
            e.printStackTrace();
        }

        return "Done";
    }

    public void onPreExecute(){
        Context context = network.this;
        SharedPreferences prefs = context.getSharedPreferences("PREFS_NAME");

        doInBackground();
    }

    public void getPrefs (Context context){         
        w1= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W1, "00"));
        w2= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W2, "00"));
        w3= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W3, "00"));
        w4= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W4, "00"));
        w5= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W5, "00"));
        w6= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W6, "00"));
        w7= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W7, "00"));
        sendbeg= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(SENDBEG, "00"));
        sendend= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(SENDEND, "00"));
    }
}

This is the snippet, my problem is about: I have no errors, except to a nullPointerException (caused by getPrefs(null);?) on Start. Can someone help me?

I already read a lot of context explanations and even the Android developer site I visited, but I don’t get, WHAT context is, and what it exactly does.

EDIT: Thanks for the fast answers, but it still does not work. I put now the whole activity in here, with one sample you gave me, but I get errors now. Is it up to me, or does Eclipse hate me?

EDIT2: I call the doInBackground on my own, because it does not start on HIS own.

EDIT3: Worked around now, it works ^^ I am calling getPrefs from my main activity directly, and then network from getPrefs… fine so far, but: I’m getting this NetworkOnMainThreadException error. What to do now?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:28:20+00:00Added an answer on June 17, 2026 at 2:28 pm

    Try this:

    1) Define a local variable in your Async class(in your case which is network class)

    Context context;
    

    2) Create constructor in your Async Task to accept context:

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

    3) Now, in onPreExecute() while calling getPrefs, call like this :

    getPrefs(context);
    

    4) You can call this class from your MainActivity like this:

    network networkObj= new network(MainActivity.this);
    networkObj.execute(url);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a SharedPreferences object from the PreferenceManager but don't want
I'm currently trying to store and restore a string from the shared preferences but
I am trying to access shared preferences from a service. I have used the
I have a app with 2 tabbed activities, and I'm trying to get SharedPreferences
After trying to get the mercurial repository explorer setup on my shared hosting with
I am trying to create a shared library in linux I get the following
I'm trying to get information from a cookie into Alfresco Share. I have a
I'm still learning Java. I'm trying to get use SharedPreferences to set the url
I am a noob to android development and I am trying to use shared
This is my code: package com.example.sharedprefs; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.