0

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;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);


        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 dir = new File (root.getAbsolutePath() + "/Bonbon info");
                dir.mkdirs();
                File f = new File(dir, "test.txt");

                StringBuilder text = new StringBuilder();

                try {
                    BufferedReader br = new BufferedReader(new FileReader(f));
                    String line;

                    while ((line = br.readLine()) != null) {
                        text.append(line);
                        text.append('\n');
                    }
                }
                catch (IOException e) {

                }

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


                }
        });
    }


    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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Goran
  • 1,239
  • 4
  • 23
  • 36

3 Answers3

1

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..

Community
  • 1
  • 1
Dinash
  • 3,027
  • 4
  • 32
  • 45
  • 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](http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background-service/) will help you to solve your problem... – Dinash Sep 11 '11 at 15:18
0

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).

jmc34
  • 810
  • 3
  • 10
  • 22
  • 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 08:04
0

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 {
    @Override
    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)
                break;
            default:
                super.handleMessage(msg);
        }
    }
}

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 {
        mService.send(msg);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
jmc34
  • 810
  • 3
  • 10
  • 22