1

MainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anaekran);
        Thread t = new Thread(new Runnable() {
            public void run() {
                String smsMsj = getIntent().getStringExtra("sms");
                if(smsMsj != null){
                    Toast.makeText(getApplication(), smsMsj, 2).show();
                }
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t.start();
    }
}

SmsReceiver.java

public class SmsReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Bundle mesaj = intent.getExtras();
        SmsMessage[] smsMessage = null;
        String msj = "";

        if(mesaj!= null){
            Object[] pdus = (Object[])mesaj.get("pdus");
            smsMessage = new SmsMessage[pdus.length];
            for(int i = 0; i < pdus.length; i++){
                smsMessage[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                msj = smsMessage[i].getMessageBody();
            }

            Intent mIntent = new Intent(context, SmsReceiver.class);
            mIntent.putExtra("sms", msj);
        }
    }
}

My receiver working correctly but i have one problem when message coming i want to show on my MainAcitivty toast, so i create mIntent in receiver class, and then im use putExtra method.

But not working, sory for my bad english and thank you :)

Kara
  • 6,115
  • 16
  • 50
  • 57
kibar
  • 822
  • 3
  • 17
  • 37

1 Answers1

1

Perhaps using explicit Intent and starting it could help you, hm? :)

Intent mIntent = new Intent(context, MainActivity.class);
mIntent.putExtra("sms", msj);
context.startActivity(mIntent);

Your mistake is that you construct your new Intent with SmsReceiver.class (but you need to launch MainActivity) and that you do not start any activity with such an intent.

Edit 1: Also, pay attention - you are trying to run a toast inside your worker thread. This is not possible. Remove your anonymous Thread and move your toast code to your onCreate(Bundle):

protected void onCreate(Bundle saveState){  
    ....
    String smsMsj = getIntent().getStringExtra("sms");
    if(smsMsj != null){
        Toast.makeText(getApplication(), smsMsj, 2).show();
    }
    ....
}

Edit 2: Moreover, your duration parameter in Toast.makeText(..) is set to 2. This does not correspond to any magic constant in Toast class. You have to use one of the constants: Toast.LENGTH_LONG or Toast.LENGTH_SHORT. So, rewrite your code to:

Toast.makeText(getApplication(), smsMsj, Toast.LENGTH_SHORT);
Drew
  • 3,307
  • 22
  • 33
  • 1
    he still wont see the toast because he is trying to show the toast in a non-ui thread though – tyczj Mar 27 '14 at 18:43
  • Yeah, You are absolutely correct. I did not notice he's starting a toast in his worker thread. I corrected my answer – Drew Mar 28 '14 at 05:35