Android Use database in broadcast receiver
<p>I have a code that uses a Database helper that extends <code>SQLiteOpenHelper</code>
Also in the <code>MainActivity</code> I have this</p>

<pre><code>DatabaseHelper TouliosDB=new DatabaseHelper(this);
</code></pre>

<p>and later </p>

<pre><code>TouliosDB = new DatabaseHelper(this);
</code></pre>

<p>Then I have one class tha extends <code>BroadcastReceiver</code> and I have it set to import data based on the received sms.
The code works correctly as far as the reading sms and geting the data. </p>

<p>My problem is that I don't know how to use the database from the <code>MainActivity</code> to the <code>BroadcastReceiver</code>. </p>

<p>This is the BroadcastReceiver class were i try to use the database TDB</p>

<p>public class  IncomingSMSReceiver extends  BroadcastReceiver{</p>

<pre><code>private static final String SMS_RECEIVED ="android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context _context, Intent _intent) {

    if (_intent.getAction().equals(SMS_RECEIVED)) {
        Bundle bundle = _intent.getExtras();
        if (bundle != null) {

            Object[] pdus = (Object[]) bundle.get("pdus");

            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i &lt; pdus.length; i++)
                messages[i] = SmsMessage
                        .createFromPdu((byte[]) pdus[i]);
            for (SmsMessage message : messages) {
                String strPhoneNo = message.getOriginatingAddress();
                String msg = message.getMessageBody();
                if (msg.startsWith("02"))
                {
                    Toast.makeText(_context, "Whats up!!", Toast.LENGTH_LONG).show();
                }
                if (msg.startsWith("01")){
                     try {
                        final String[] temaxismeno_sms = msg.split(":");
                        //eisagoghfititi(String am,String onoma,String epitheto,String examino)
                        // temaxismeno_sms[0] einai o kodikos eisagoghs apo to menu
                        //boolean perastike = myDb.eisagoghfititi(temaxismeno_sms[1],temaxismeno_sms[2],temaxismeno_sms[3],temaxismeno_sms[4]);
                        //if (perastike=true){
                        //else
                        //{
                        //    Toast.makeText(_context, "H eisagwgh apetixe", Toast.LENGTH_LONG).show();
                          class MyBroadcast extends BroadcastReceiver {
                             @Override
                             public void onReceive(final Context context, Intent intent) {
                                 HandlerThread handlerThread =  new HandlerThread("database_helper");
                                 handlerThread.start();
                                 Handler handler =  new Handler(handlerThread.getLooper());
                                 handler.post(new Runnable() {
                                     @Override
                                     public void run() {
                                         DatabaseHelper TouliosDB = new DatabaseHelper(context);
                                         TouliosDB.eisagoghfititi(temaxismeno_sms[1],temaxismeno_sms[2],temaxismeno_sms[3],temaxismeno_sms[4]);
                                         // have more database operation here
                                     }
                                 });
                             }
                         }
                        Toast.makeText(_context, "Egine eisagwgh fititi!!", Toast.LENGTH_LONG).show();
                        String message1 = "H Eisagwgh egine sthn vash.";// minima pou tha stalthei
                        SmsManager sms = SmsManager.getDefault();
                        sms.sendTextMessage(strPhoneNo, null, message1, null, null);
                        Toast.makeText(_context, "O fititis idopiithike", Toast.LENGTH_LONG).show();
                        }
                    catch (Exception e)
                        {
                        Toast.makeText(_context, "SMS failed, please try again.",
                                Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                        }
            }}
        }
    }
}
</code></pre>

<p>}</p>

<p>Database code</p>

<pre><code>package toulios.ptixiakh.toulios;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME="Toulios.db";
public static final String TABLE_NAME="Foitites_table";
public static final String Col_AM="AM";
public static final String Col_ONOMA="ONOMA";
public static final String Col_EPITHETO="EPITHETO";
public static final String Col_EXAMINO="EXAMINO";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
   // SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME+"(AM INTEGER PRIMARY KEY,ONOMA TEXT,EPITHETO TEXT, EXAMINO INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
    onCreate(db);
}
public boolean eisagoghfititi(String am,String onoma,String epitheto,String examino)
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(Col_AM,am);
    contentValues.put(Col_ONOMA,onoma);
    contentValues.put(Col_EPITHETO,epitheto);
    contentValues.put(Col_EXAMINO,examino);
    long result = db.insert(TABLE_NAME,null,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}
</code></pre>

<p>}</p>