Am developing an android application in that app i have database table... When a particular formatted message comes to an phone, it should get splitted the value of that message carries should go and insert into the created table in database...
The message format is: Lat:12.537347487 Lan:77.674875980 Accry:4.0 Fri May 09 10:28:34 IST 2014
This value of the formatted message wants to get stored in database table via onReceive function... The message should be get split and the values of Lat,Lan,Accry,Date and Time should be store in the database...
SmsReceiver.java:
package com.example.sample2;
import java.util.ArrayList;
import java.util.Calendar;
import com.example.sample2.SecureMessagesActivity;
//import org.secure.sms.SmsInfo;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
public static final String SMS_EXTRA_NAME = "pdus";
public static final String SMS_URI = "content://sms";
public static final String ADDRESS = "address";
public static final String MOBILE = "Mobile";
public static final String BODY = "body";
public static final String LAT = "lat";
public static final String LAN = "lan";
public static final String ACCURACY = "Accry";
public static final String DATE = "Date";
public static final String TIME = "Time";
public static final String SEEN = "seen";
public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;
public static final int MESSAGE_IS_NOT_READ = 0;
public static final int MESSAGE_IS_READ = 1;
public static final int MESSAGE_IS_NOT_SEEN = 0;
public static final int MESSAGE_IS_SEEN = 1;
// static ArrayList<SmsInfo> listSms = new ArrayList<SmsInfo>();
// Change the password here or give a user possibility to change it
// public static final byte[] PASSWORD = new byte[]{ 0x20, 0x32, 0x34, 0x47, (byte) 0x84, 0x33, 0x58 };
// ArrayList<String> smsList = new ArrayList<String>();
@Override
public void onReceive( Context context, Intent intent )
{
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
if(sms.getMessageBody().contains("Lat:"))
{
String[] splitted = sms.getMessageBody().split("\n");
String body= splitted[0];
String[] mobval=body.split(":");
String Mobile=mobval[1];//Mob Value
body= splitted[2];
String[] latval=body.split(":");
String lat=latval[1];//Lat Value
body = splitted[3];
String[] lanval=body.split(":");
String lan=lanval[1];//Lan Value
body = splitted[4];
String[] Accval=body.split(":");
String Accry=Accval[1];//Accry value
body= splitted[5];
String Dateval=body.substring(4,10);
String Timeval=body.substring(11,20);
String Date=Dateval; //Date value
String Time=Timeval; //Time value
String address = sms.getOriginatingAddress();
messages += "SMS from" + address + ":\n";
//—display the new SMS message—
Toast.makeText(context, messages, Toast.LENGTH_SHORT).show();
insertintoDB(context, Mobile,lat,lan,Accry,Date,Time);
//putSmsToDatabase(Mobile,lat,lan,Accry,Date,Time, context );
abortBroadcast();
}
}
}
}
private void insertintoDB(Context context, String mobile, String lat, String lan, String date,String time,String accuracy) {
// final SharedPreferences prefs = getSharedPreferences(Const.COMMON_SHARED, Context.MODE_PRIVATE);
//String userId = prefs.getString(Const.COMMON_USERID, null);
DataBaseHelper dbHelper= new DataBaseHelper(context);
// dbHelper.openDataBase();
dbHelper.insertData(mobile, lat, lan, date,accuracy,time);
dbHelper.close();
}
DataBaseHelper.java:
package com.example.sample2;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DataBaseHelper extends SQLiteOpenHelper {
//public static final String SMS_URI = “/data/data/org.secure.sms/databases/”;
public static final String DB_NAME= "message";
public static final String TABLE_NAME= "sms2";
public static final int version =1;
public static final String ADDRESS = "address";
public static final String MOBILE = "Mobile";
public static final String BODY = "body";
public static final String LAT = "lat";
public static final String LAN = "lan";
public static final String ACCURACY = "Accry";
public static final String DATE = "Date";
public static final String TIME = "Time";
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
String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+MOBILE+"varchar(13),"+LAT+" double precision, "+LAN+" double precision, "+ACCURACY+" double,"+DATE+ "DATETIME DEFAULT CURRENT_TIMESTAMP,"+TIME+ "DATETIME DEFAULT CURRENT_TIMESTAMP)";
db.execSQL(CREATE_TABLE);
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;
}
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);
}
public void insertData(String mobile, String lat, String lan, String date, String accuracy,String time) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MOBILE, mobile);
values.put(LAT, lat);
values.put(LAN, lan);
values.put(ACCURACY, accuracy);
values.put(DATE, date);
values.put(TIME, time);
db.insert(DB_NAME, null, values);
db.close();
}
}