jeudi 8 janvier 2015

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

I am doing an Android Project.What I want to do is read a new sms whenever it is received & store it in database.I have done upto this.I have a database "DBBANKING.db". The corresponding code is : 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/http://ift.tt/1nnj9cL”;
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);
}

}


It works fine.Whenever a new sms comes it stores the informartion in database. Now I have another table in database where I am storing few SMS Numbers.(the name of the field where I am storing the smsnumbers is SMSNUMBERS. Now I want whenever a new sms comes into my phone it will check over SMSNUMBER feild of database "DBBANKING.db".If the number matches with the numbers I have given in the field then it will store the sms body in database else it will not store that. How can I do that?Help out please!


Aucun commentaire:

Enregistrer un commentaire