Hi!
Are you declaring your receiver on the manifest or somewhere else on the
code? I'm copying this from an example I Googled, so it might not be
exactly what you need, but here are two ways to declare the receiver:
On android_manifest.xml
<receiver
android:name=". IncomingSms"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
On your code
// Register a broadcast receiver
IntentFilter intentFilter = new
IntentFilter("android.intent.action.DATA_SMS_RECEIVED");
intentFilter.setPriority(10);
intentFilter.addDataScheme("sms");
intentFilter.addDataAuthority("*", "6734");
registerReceiver(incomingSms, intentFilter);
Marina
On Wed, Dec 28, 2016 at 3:24 AM, Navid Arfaie <[email protected]>
wrote:
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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
> <https://groups.google.com/d/msgid/android-developers/0fa831c4-5c2e-4151-8dbf-afe51f73b880%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To post to this group, send email to [email protected].
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/CACaNmX0mD%3DGEKGY7_8dbtOXtkhTqAqZa95JYmuB%3DYiOwcZ_88g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.