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?
Try this:
1) Define a local variable in your Async class(in your case which is network class)
2) Create constructor in your Async Task to accept context:
3) Now, in
onPreExecute()while callinggetPrefs, call like this :4) You can call this class from your MainActivity like this: