如何读取Android的特定消息格式,并存储在SQLite数据库? [英] How to read the particular message format in android and store in sqlite database?

查看:192
本文介绍了如何读取Android的特定消息格式,并存储在SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在开发一个GPS跟踪Android应用程序,我得到的GPS从客户端短信坐标。在服务器端,我需要输入短信存储在数据库中。在我的应用我得到的所有传入邮件数据库,但我只需要特定的消息格式存储在数据库中。请帮我解决这个问题。先谢谢了。

Am developing an GPS tracking android application, I get the GPS coordinates from the client side as sms. In server side i need to store the incoming sms in database. In my application i'm getting all the incoming messages to database, but i need to store only the particular message format in database. Please help me to solve this problem. Thanks in advance.

消息格式:

Lat:12.5678994
Lan:77.7847599
Accry:4.0

以上是我从客户获取消息格式,我只需要这个存储在数据库中,并非所有的传入消息。

The above is the message format which am getting from client, i need to store only this in database, not all the incoming messages.

MainActivity.java:

MainActivity.java:

public class SecureMessagesActivity extends Activity implements OnClickListener, OnItemClickListener
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setTheme( android.R.style.Theme_Light );
        setContentView(R.layout.main);
        this.findViewById( R.id.UpdateList ).setOnClickListener( this );
    }

    ArrayList<String> smsList = new ArrayList<String>();

    public void onItemClick( AdapterView<?> parent, View view, int pos, long id ) 
    {
        try 
        {
            //Retriving Lat Lan from msg body
                String[] splitted = smsList.get( pos ).split("\n"); 
            String sender = splitted[0];

            String data= splitted[2];
            String[] latval=data.split(":");
            String lat=latval[1];//Lat Value 

            String data1 = splitted[3];
            String[] lanval=data1.split(":");
            String lan=lanval[1];//Lan Value

            Toast.makeText( this, lat, Toast.LENGTH_SHORT ).show();
            Toast.makeText( this, lan, Toast.LENGTH_SHORT ).show();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public void onClick( View v ) 
    {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null);
        //Cursor cursor = contentResolver.query(Uri.parse("content://sms/inbox"),new String[]{phoneLookUp.DISPLAY_NAME}, condition, null, null, null);

        int indexBody = cursor.getColumnIndex( SmsReceiver.BODY );
        int indexAddr = cursor.getColumnIndex( SmsReceiver.ADDRESS );
        //int indexDate = cursor.getColumnIndex( SmsReceiver.DATE );

        if ( indexBody < 0 || !cursor.moveToFirst() ) return;

        smsList.clear();

        do
        {
            String str = getContactName(getApplicationContext(),cursor.getString(cursor.getColumnIndex(SmsReceiver.ADDRESS)))+":"
                    + cursor.getString( indexAddr ) + "\n" 
                    + cursor.getString( indexBody );
            smsList.add( str );
        }
        while( cursor.moveToNext() );


        ListView smsListView = (ListView) findViewById( R.id.SMSList );
        smsListView.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, smsList) );
        smsListView.setOnItemClickListener( this );
    }

    public String getContactName(Context context, String phoneNumber) {
        ContentResolver cr = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        Cursor cursor = cr.query(uri,
                new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
        if (cursor == null) {
            return null;
        }
        String contactName = null;
        if (cursor.moveToFirst()) {
            contactName = cursor.getString(cursor
                    .getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return contactName;
    }

}

SMSReceiver.java:

SMSReceiver.java:

公共类SmsReceiver扩展广播接收器
{

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;

// Change the password here or give a user possibility to change it

//公共静态最后字节[] PASSWORD =新的字节[] {为0x20,0x32,0x34,0X47(字节)的0x84,0x33,将0x58};

// public static final byte[] PASSWORD = new byte[]{ 0x20, 0x32, 0x34, 0x47, (byte) 0x84, 0x33, 0x58 };

public void onReceive( Context context, Intent intent ) 
{
    // Get SMS map from Intent
    Bundle extras = intent.getExtras();

    String messages = "";

    if ( extras != null )
    {
        // Get received SMS array
        Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );

        // Get ContentResolver object for pushing encrypted SMS to incoming folder
        ContentResolver contentResolver = context.getContentResolver();

       // for ( int i = 0; i < smsExtra.length; ++i )
        //{
            SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[0]);

            String body = sms.getMessageBody().toString();
            if(sms.getMessageBody().contains("Lat:"))
            {
            String address = sms.getOriginatingAddress();
            messages += "SMS from " + address + " :\n";                    
            messages += sms.getMessageBody().toString() + "\n";
            putSmsToDatabase( contentResolver, sms );
            Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
            this.abortBroadcast();              }
        //}

        // Display SMS message
        //Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
    }

    // WARNING!!! 
    // If you uncomment next line then received SMS will not be put to incoming.
    // Be careful!

}

private void putSmsToDatabase( ContentResolver contentResolver, SmsMessage sms )
{
    // Create SMS row
    ContentValues values = new ContentValues();
    values.put( ADDRESS, sms.getOriginatingAddress() );
    values.put( DATE, sms.getTimestampMillis() );
    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 );
    try
    {
        String encryptedPassword= sms.getMessageBody().toString();


        //String encryptedPassword = StringCryptor.encrypt( new String(PASSWORD), sms.getMessageBody().toString() ); 
        values.put( BODY, encryptedPassword );


    }
    catch ( Exception e ) 
    { 
        e.printStackTrace(); 
    }

    // Push row into the SMS table
    contentResolver.insert( Uri.parse( SMS_URI ), values );
}

}

推荐答案

您可以使用

if(messages.getMessageBody().contains("Lat:")) {

     //Write your database insertion code here

}

我有that..May不正确one..But还是一个解决方案..
尝试用即对于例如,一个特殊的头发送短信#####,并检查#####这样的..

I have one solution for that..May not be the correct one..But still.. Try sending the sms with a special header ie for eg "#####" and check for the "#####" like this..

if(messages.getMessageBody().contains("#####")) {

      //Write your database insertion code here

}

这篇关于如何读取Android的特定消息格式,并存储在SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆