Android在广播接收器中使用数据库



我有一个代码,它使用扩展SQLiteOpenHelper的数据库帮助程序同样在MainActivity中,我有这个

DatabaseHelper TouliosDB=new DatabaseHelper(this);

以及后来的

TouliosDB = new DatabaseHelper(this);

然后我有一个扩展BroadcastReceiver的类,并将其设置为基于接收到的短信导入数据。该代码可以正确地读取短信和获取数据。

我的问题是,我不知道如何使用从MainActivityBroadcastReceiver的数据库。

这是BroadcastReceiver类,如果我尝试使用数据库TDB

公共类IncomingMSReceiver扩展BroadcastReceiver{

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 < 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();
                        }
            }}
        }
    }
}

}

数据库代码

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;
}

}

那么您想在BroadcastonReceive回调中进行数据库操作吗?如果我理解正确的话。这里有一个简单的例子。

public 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 TDB = new DatabaseHelper(context);
                TDB.eisagoghfititi(temaxismeno_sms[1],temaxismeno_sms[2],temaxismeno_sms[3],temaxismeno_sms[4]);
                // have more database operation here
            }
        });
    }
}

更复杂的方法是在onReceive回调处启动服务。并在您的服务中执行数据库操作。

相关内容

最新更新