如何拆分传入的短信并存储在数据库中

时间:2014-05-03 09:35:42

标签: android sqlite

正在开发一个Android应用程序,我需要拆分传入的短信并使用解析我需要在android中的数据库中存储消息。 消息格式:

Lat:12.567889000999
Lan:77.456667668899
accry:4.0

上面是我要解析的消息格式,并将消息拆分并存储在数据库中。

这是我创建的用于存储消息的表。

create table attandance(id serial Not null,lat double precision, lan double precision, accuracy double precision, timeres timestamp);

请帮我解决这个问题。

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

}
}

1 个答案:

答案 0 :(得分:0)

您可以使用substring()

str.substring(startIndex, endIndex); 

所以,

string lat = "Lat:12.567889000999";
string result = lat.substring(lat.indexOf(':'),lat.length());