Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

sms - How to read the message content of a new in coming message in android?

I want to read the message body of a new incoming SMS in android, programmatically.

I tried something but that doesn't return any contents:

Uri uri = Uri.parse("content://sms/inbox");
        ContextWrapper context = null;      
        Cursor c = context.getContentResolver().query(uri, null, null ,null,null);      
        String body = null; 
        String number=null;
        if(c.moveToFirst()) {        
           body = c.getString(c.getColumnIndexOrThrow("body")).toString();
           number = c.getString(c.getColumnIndexOrThrow("address")).toString();
        }
        c.close(); 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have posted some sample programs about this on my class website. Here is the example Read SMS Example Here is a snippet of code. Basically your can register a broadcast receiver to listen for SMS_Receive and check out the following.

Intent intent = getIntent();
    Bundle bundle = intent.getBundleExtra("mySMS");

    if (bundle != null) {
        Object[] pdus = (Object[])bundle.get("pdus");
        SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[0]);
        Log.i("mobile.cs.fsu.edu", "smsActivity : SMS is <" +  sms.getMessageBody() +">");

        //strip flag
        String message = sms.getMessageBody();
        while (message.contains("FLAG"))
            message = message.replace("FLAG", "");

        TextView tx = (TextView) findViewById(R.id.TextBox);
        tx.setText(message);            
    } else
        Log.i("mobile.cs.fsu.edu", "smsActivity : NULL SMS bundle");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...