views:

44

answers:

1

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?

+1  A: 

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
still having problem with passing the intent. can you provide some examples?
conandor
@conandor, Added some code that might guide you better, though i couldn't test it, don't have access to my dev machine. :(
st0le
Yes, I have the same code. but It keep launching the new textview upon receiving the message
conandor
@conandor,Post your code, maybe that'll help...
st0le
Problem solved. On receiving part codes as below. Thankx st0le!
conandor
public void onNewIntent(Intent intent){ String msg = intent.getStringExtra("SMSBODY");}
conandor
@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