每当有新短信存储到 database.But 中时,我都会完成,我想在收到特定号码时将短信存储到数据库中

I have done whenever a new sms come it stored into the database.But,i want to store sms into the databse when received particular number

我正在做一个 Android 项目。我想要做的是在收到新 SMS 时读取它并将其存储在数据库中。我已经做到了这一点。我有一个数据库 DBBANKING.db.

对应代码在Reveiversms.java:

package com.example.projdatabsesms;

/*Create class with extending BroadcastReceiver so in onReceive method we can write code which will read each new incoming sms when it comes to mobile.*/

public class Reveiversms extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
//—get the SMS message passed in—
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messages = "";
if (bundle != null)
{
//—retrieve the SMS message received—
Object[] smsExtra = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[smsExtra.length];

for (int i=0; i<msgs.length; i++)
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
//take out content from sms
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();

messages += "SMS from " + address + " :\n";
messages += body + "\n";

putSmsToDatabase(sms, context );
}
//—display the new SMS message—
//Toast.makeText(context, messages, Toast.LENGTH_SHORT).show();

}

}

private void putSmsToDatabase( SmsMessage sms, Context context )
{
DataBaseHelper dataBaseHelper = new DataBaseHelper(context);

SQLiteDatabase db = dataBaseHelper.getWritableDatabase();

String mydate =java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
// Create SMS row
ContentValues values = new ContentValues();

values.put("address", sms.getOriginatingAddress().toString() );
values.put("date", mydate);
values.put("body", sms.getMessageBody().toString());


db.insert("datatable", null, values);

db.close();

}
}

DatabaseHelper.java:

public class DataBaseHelper extends SQLiteOpenHelper {

//public static final String SMS_URI = “/data/data/org.secure.sms/databases/”;
public static final String db_name = "DBBANKING.db";
public static final int version =1;
Context context;
public DataBaseHelper(Context context) {
super(context, db_name, null, version);
// TODO Auto-generated constructor stub
this.context =context;
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

db.execSQL("create table datatable(address varchar(10), date varchar(10), body varchar(30))");
Toast.makeText(context, "database created", Toast.LENGTH_LONG).show();
Log.i("dbcreate", "DATABASE HAS CREATED");
}

public boolean checkDataBase(String db) {

SQLiteDatabase checkDB = null;

try {
String myPath = "data/data/"+ context.getPackageName() +"/databases/" + db;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);

} catch (SQLiteException e) {

// database does’t exist yet.

} catch (Exception e) {

}

if (checkDB != null) {

checkDB.close();

}

return checkDB != null ? true : false;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
if (oldVersion >= newVersion)
return;

if (oldVersion == 1) {
Log.d("New Version", "Datas can be upgraded");
}

Log.d("Sample Data", "onUpgrade : " + newVersion);
}

}

它工作正常。每当有新的 SMS 到来时,它会将信息存储在数据库中。现在我在数据库中有另一个 table,我在其中存储 SMS 号码。我存储 smsnumbers 的字段名称是 SMSNUMBERS.

现在我想要这样的功能,每当新的 SMS 进入我的 phone 时,它都会检查 DBBANKING.dbSMSNUMBER 字段。如果号码与我在字段中给出的号码匹配,那么它会将 SMS 正文存储在数据库中,否则不会存储它。我该怎么做?

这应该对你有帮助。

在您的 DatabaseHelper class 中创建一个方法,如下所示。

public ArrayList<String> getAllSMSNumbers() {
        ArrayList<String> listOfSms = null;

        String query = "SELECT * FROM " + TABLE_SMS_NUMBERS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            listOfCards = new ArrayList<String>();
            do {
                listOfCards.add(cursor.getInt(cursor
                        .getColumnIndex(KEY_SMS_NUMBER));
            } while (cursor.moveToNext());
        }
        return listOfCards;
    }

Reveiversms class 中创建 DatabaseHelper class 的实例。 在您的 onRecieve() 方法中执行此操作。

DatabaseHelper db=new DatabaseHelper(context);
ArrayList<String> listOfSms=db.getAllSMSNumbers();
    if(!listOfSms.contains(smsNumber)){
        putSmsToDatabase(sms, context );
    }

其中 smsNumber 是您刚刚收到的短信的发件人号码。

试试这个 来自广播意图

if (bundle != null) {
   final Object[] pdusObj = (Object[]) bundle.get("pdus");
    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
}

// 检查号码是否与该特定号码匹配并存储在 DB