Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: pass variables from Activity to BroadcastReceiver
    primarykey
    data
    text
    <p>I have some problem with passing throught my variable from Activity to the BroadcastReceiver... Here is my code:</p> <p>here is my Broadcast receiver code... I try to catch SMS from one phone number which I have got from my Activity...</p> <pre><code>public class SMSMonitor extends BroadcastReceiver { private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; public static String phone_number = ""; public static String msg_body = ""; public static final String SMS_EXTRA_NAME = "pdus"; @Override public void onReceive(Context context, Intent intent) { String phone = intent.getExtras().getString("trusted_num"); if (intent != null &amp;&amp; intent.getAction() != null &amp;&amp; ACTION.compareToIgnoreCase(intent.getAction()) == 0) { Object[] pduArray = (Object[]) intent.getExtras().get("pdus"); SmsMessage[] messages = new SmsMessage[pduArray.length]; for (int i = 0; i &lt; pduArray.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]); } phone_number = messages[0].getDisplayOriginatingAddress(); msg_body = messages[0].getMessageBody(); System.out.println("Phone number: "+phone_number); System.out.println("Phone entered: "+phone); } } } </code></pre> <p>Here is my Activity code:</p> <pre><code>public class Settings extends Activity implements OnClickListener{ private Button btn_save; private EditText txt_phone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); //set Save button btn_save = (Button)findViewById(R.id.btn_save); txt_phone = (EditText)findViewById(R.id.et_phone); btn_save.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_settings, menu); return true; } @Override public void onClick(View v) { if (v == btn_save) { try { String phone_num = txt_phone.getText().toString(); Intent i = new Intent(Settings.this, SMSMonitor.class); i.putExtra("trusted_num", phone_num); sendBroadcast(i); } catch(Exception e) { System.out.println("Error: "+e.getLocalizedMessage()); } } } } </code></pre> <p>In this code I have text field for entering the phone number, which I need to pass to the BroadcastReceiver with <code>intent.putExtra()</code> method, but in LogCat I see, that variable didnot pass:</p> <pre><code>07-25 18:43:57.382: I/System.out(14245): Phone number: +37129690449 07-25 18:43:57.382: I/System.out(14245): Phone entered: null </code></pre> <p>So what I am doing wrong here?</p> <p><strong>UPD</strong> Maybe code is not correct, but it works for me...</p> <pre><code>public void onReceive(Context context, Intent intent) { phone = intent.getExtras().getString("trusted_num");//get trusted phone number from Settings screen //receive SMS if (intent != null &amp;&amp; intent.getAction() != null &amp;&amp; ACTION.compareToIgnoreCase(intent.getAction()) == 0) { Object[] pduArray = (Object[]) intent.getExtras().get("pdus"); SmsMessage[] messages = new SmsMessage[pduArray.length]; for (int i = 0; i &lt; pduArray.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]); } phone_number = messages[0].getDisplayOriginatingAddress(); msg_body = messages[0].getMessageBody(); System.out.println("Phone number: "+phone_number); } //check if number is not null if (phone != null &amp;&amp; phone != "") { System.out.println("Phone entered: "+phone); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload