Вопрос

am developing an android application in which i receive the particular message format, then i want to split that message and i have to store in database. But, now in my application the whole message body is storing in database.Please help me to solve this problem, thanks in advance.

Message Format:

+919978650055
Lat:12.445776890098
Lan:77.655677889000
Accry:4.0
Fri May 09 12:29:21 IST 2014

This is the message format which am receiving, now i want only the values which are stored in lat,lan,accry,date,time and send to database.

SmSReceiver.java:

    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 PERSON = "person";
        public static final String DATE = "date";
        public static final String READ = "read";
        public static final String STATUS = "status";
        public static final String TYPE = "type";
        public static final String BODY = "body";
        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 };
        @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 body = sms.getMessageBody().toString();
            String address = sms.getOriginatingAddress();
            messages += "SMS from" + address + ":\n";
            messages += body + "\n";
            //—display the new SMS message—
            Toast.makeText(context, messages, Toast.LENGTH_SHORT).show();
            putSmsToDatabase(sms, context );
            abortBroadcast();
            }

            }

            }
            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());
// values.put( READ, MESSAGE_IS_NOT_READ );
// values.put( STATUS, sms.getStatus() );
// values.put( TYPE, MESSAGE_TYPE_INBOX );
// values.put( SEEN, MESSAGE_IS_NOT_SEEN );

db.insert(SMS_URI, null, values);

db.close();

}
}

        }

below is the code which i used to split the body,but its not working

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

        }

        }

    }
Это было полезно?

Решение

You can split the message from: like below..

StringTokenizer tokenizer = new StringTokenizer(message, ":");
int numberOfTokens = tokenizer.countTokens();
String[] splitArr = new String[numberOfTokens];
splitArr[0] = tokenizer.nextToken();
splitArr[1] = tokenizer.nextToken();
String lat=splitArr[0];
String lng=splitArr[1];

Similarly you can do it for other values too..

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top