0

I use code from http://mobiforge.com/developing/story/sms-messaging-android as reference. I added scrollview and it show the append text upon sms send out.

however i have problem append incoming sms text in the same scrollview. how can i solve it? do i need to use thread, service?

conandor
  • 3,637
  • 6
  • 29
  • 36

1 Answers1

4

Use a Broadcast Receiver to Hook onto incomming SMS....Fire an Intent (with SMS Body as an Extra) to Trigger your Activity (your link will help with that)...in the onStart() or onNewIntent() you grab the Extra and update your UI...

Another Method would be to use a ContentObserver for content://sms/ but that's advised against unless your sure the Messaging App will intercept the SMS.

Untested Code!

Intent intent = new Intent(context,YourActivity.class); //context from onRecieve(context,intentData)
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK); //required if ur app is not currently running.
intent.putExtra("SMSBODY",smsbody); //get smsbody from the getMessageBody() (from your link)
context.startActivity(intent);

In your Activity...In onStart() or onNewIntent()

Intent intent = getIntent();
if(intent.getStringExtra("SMSBODY") != null)
{
String msg = intent.getStringExtra("SMSBODY");
//append msg to scroll view
}
st0le
  • 33,375
  • 8
  • 89
  • 89
  • still having problem with passing the intent. can you provide some examples? – conandor Sep 11 '10 at 15:59
  • @conandor, Added some code that might guide you better, though i couldn't test it, don't have access to my dev machine. :( – st0le Sep 11 '10 at 17:06
  • Yes, I have the same code. but It keep launching the new textview upon receiving the message – conandor Sep 12 '10 at 02:47
  • Problem solved. On receiving part codes as below. Thankx st0le! – conandor Sep 18 '10 at 11:34
  • public void onNewIntent(Intent intent) { String msg = intent.getStringExtra("SMSBODY"); } – conandor Sep 18 '10 at 11:37
  • @conandor, that's great, but make sure you put a `null` check, onNewIntent() maybe called for other reasons as well, and those `Intent`'s wont have your extra. – st0le Sep 18 '10 at 11:55
  • What if my activity alread started and I just want to update it? – complez Oct 22 '11 at 09:42
  • 1
    @complez, Your activity should be `singleInstance` and I believe `onNewIntent` will be called in that case. – st0le Oct 26 '11 at 10:40
  • To add to the comment above by @st0le, if you make use of `startActivityForResult()` in your app, then consider http://stackoverflow.com/questions/5118525/onactivityresult-do-not-fire-if-launch-mode-of-activity-is-singleinstance .. in short, use launchMode `singleTask` instead of `singleInstance` – sga4 Oct 13 '15 at 21:41