I Create a BroadcastReciver and want to call a method when SMS Recived! 
this code work correctly in other classes but not fire in BroadcastReciver.


*My BroadcastReciver.java:*


public class IncomingSms extends BroadcastReceiver {
String smsBody;

private SmsListener smsListener;

public interface SmsListener {
    void PassActivate();
};

public void setSmsListener(SmsListener listener) {
    smsListener = listener;
}

public static final String SMS_BUNDLE = "pdus";
@Override
public void onReceive(Context context, Intent intent) {

    String address = "";
    Bundle intentExtras = intent.getExtras();
    if (intentExtras != null) {
        Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
        String smsMessageStr = "";
        for (int i = 0; i < sms.length; ++i) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

            smsBody = smsMessage.getMessageBody().toString();
            address = smsMessage.getOriginatingAddress();
        }

        String strOrig = smsBody;

        try {
            if (strOrig.contains("_Activated_"))  {
                if(smsListener!=null){
                    smsListener.PassActivate();
                }

            } else {
                Toast.makeText(context, "Ops", Toast.LENGTH_LONG).show();
            }

        } catch (Exception e) {
            Toast.makeText(context, "Ops", Toast.LENGTH_LONG).show();
        }

    }

}


When SMS recive , I chek smsListener.But it's always null!


*and My Activity:*


import arfaie.navid.mei.IncomingSms.SmsListener;

public class Activate extends Activity {

private IncomingSms incomingSms;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activate);

    incomingSms = new IncomingSms();
    incomingSms.setSmsListener(smsListener);

}

SmsListener smsListener = new SmsListener() {
    @Override
    public void PassActivate() {
        Toast.makeText(getBaseContext(), "It Works", Toast.LENGTH_SHORT).show();
    }
};



-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/0fa831c4-5c2e-4151-8dbf-afe51f73b880%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to