I am working on an Android app. One of the first things the app does upon starting is to launch a listening thread. This listening thread opens a UDP socket to another application on the tablet, and listens for that other application to send data. When my listening thread receives data, I want to store the data into the SQLite database, but I cannot figure out how to do that without access to my app's Context.
I have a database helper app:
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context){
super(context, "myAppDatabase.db", null, 1);
}
@Override
public void OnCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE ...");
}
@Override
public void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE ...");
}
// my database CRUD queries
}
This DatabaseHelper class is the only place where I perform any CRUD operations on the database.
I start my thread from MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
ListenerClass myListenerClass = new ListenerClass();
Thread listenerThread = new Thread(myListenerClass);
listenerThread.setName("My Listener Thread");
listenerThread.start();
}
}
And I have my listener class:
public class ListenerClass implements Runnable {
private volatile boolean run = true;
private DataProcessingClass myDataProcessingClass;
@Override
public void run(){
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
// Open UDP socket
// Listen for data
// Process the data
myDatagramSocket.receive(myDatagramPacket);
byte[] data = myDatagramPacket.getData();
myDataProcessingClass = new DataProcessingClass();
myDataProcessingClass.processData(data);
}
public void setRun(boolean run){
this.run = run;
}
}
When my listener thread receives data, it calls another class to process that data, and store it into the database. It works up until I try to store it into the database, because I do not have access to the app's Context.
Is there any way to write to the SQLite database without access to the app's Context? If not, how can I go about getting hold of the Context for this database write?
Aucun commentaire:
Enregistrer un commentaire