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

前端 未结 5 1200
深忆病人
深忆病人 2020-12-04 20:42

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:

5条回答
  •  Happy的楠姐
    2020-12-04 20:49

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

提交回复
热议问题