Take the 2-minute tour ×

I have an app that send SMS for checking remaining MB in my data package. I have a layout with a button and a text view. When I press my button, I send a message to my phone operator. Then I have a broadcast receiver, that listens to incoming messages, and saves message body to a text file. I Want to show this text in my text view when I get answer from my operator.

This is my code:

public class bonbon3 extends Activity 

    Button btnStanje;
    Context context=this;

    public void onCreate(Bundle savedInstanceState) 

        btnStanje = (Button) findViewById(R.id.provjeriStanje);

        btnStanje.setOnClickListener(new View.OnClickListener() 
            public void onClick(View v) 
                String phoneNo = "0977";
                String message = "stanje";                 

                sendSMS(phoneNo, message); 

                Toast.makeText(getBaseContext(), "Zahtjev za provjeru stanja paketa je poslan, odgovor očekuj uskoro!", Toast.LENGTH_SHORT).show();

                File root = Environment.getExternalStorageDirectory();
                File f = new File(dir, "test.txt");

                StringBuilder text = new StringBuilder();

                try {
                    String line;

                catch (IOException e) {


               TextView tv = (TextView)findViewById(R.id.textView2);


    private void sendSMS(String phoneNumber, String message)
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);        

Now, this code I am trying to read from file, before answer SMS is received, so I know this is wrong, but I don't know how to load text to textView after I get SMS answer?

share|improve this question

3 Answers 3

hi i think this link will be very helpful for you

Reading a text file from sdcard in android is as same as reading a text file in java..

share|improve this answer
Yes, i used this code, but my question is can I delay updating textView until i recive the message ?? –  Goran Sep 11 '11 at 15:13
k i think link to update the ui from background service will help you to solve your problem... –  Dinash Sep 11 '11 at 15:18

Goran, I cannot reply inline, but if you follow my link, there is pretty much everything you need.

I followed this example almost excatly and it worked out well.

You just need to implement a message handler in your activity (instead of the service as in the example) and push the message from the service (instead of recieving it here) but except that, this is exactely the same.

So in your activity, you should have something like this :

 * Handler of incoming messages from clients.
class IncomingHandler extends Handler {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_GOT_SMS:
                // Fill your text view here using the msg.obj (you put it there)

In you service (the bit that recieves the SMS) you should have something like:

public void sendText(String sms) {
    // Create and send a message to the service, using a supported 'what' value
    Message msg = Message.obtain(null, MyActivity.MSG_GOT_SMS,O, 0, sms);
    try {
    } catch (RemoteException e) {
share|improve this answer

I think you would rather establish a messaging communication between your activity and your broadcast reciever (that is a service isn't it?).

You are bound to have a loop that will try to read the file continously until it is there whci does not feel right. Do not store the SMS in a text file send the content directly from the activity to the service using the messaging system.

For messaging between activities and services, have a look in the Android developper guide about Bound services and especially the section about messenger (your service does not need to be bound).

share|improve this answer
Ok, i will look into this, but i am noob for some thing, so is there any example, or something you can help me with ?? –  Goran Sep 11 '11 at 17:37
Sorry, did not succeed commenting here earlier, please see my other answer. –  jmc34 Sep 12 '11 at 8:04

Your Answer


