使用onReceive()拆分邮件正文

时间:2014-05-10 06:59:11

标签: android sqlite

正在开发一个Android应用程序,我收到特定的消息格式,然后我想拆分该消息,我必须存储在数据库中。但是,现在在我的应用程序中,整个邮件正在存储在数据库中。请提前帮助我解决这个问题。

邮件格式:

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

这是接收的消息格式,现在我只想要存储在lat,lan,accry,date,time中的值并发送到数据库。

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

}
}

        }

下面是我用来分割正文的代码,但它不起作用

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

        }

        }

    }

1 个答案:

答案 0 :(得分:0)

您可以将邮件从:拆分,如下所示..

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];

同样,你也可以为其他值做这件事。