I have a problem(again)…now i want to pass a String from one Activity to another.
The first Activity is SMSReceiver, i make a ListView for contact’s number and message’s here.
This is the complete code :
public class SMSReceiver extends BroadcastReceiver {
private static final int NOTIF_ID = 0;
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Bundle bundle = arg1.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "You Get New SMS from " + msgs[i].getOriginatingAddress();
str += " :"; str += msgs[i].getMessageBody().toString();
str += "\n";
Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
NotificationManager nm = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
String tickerText = str;
Notification notif = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis());
notif.flags = Notification.FLAG_AUTO_CANCEL;
String contentTitle = msgs[i].getOriginatingAddress();
String contentText = msgs[i].getMessageBody().toString();
Intent intent = new Intent(arg0, SMSReply.class);
PendingIntent pi = PendingIntent.getActivity(arg0, 0, intent, 0);
notif.setLatestEventInfo(arg0, contentTitle, contentText, pi);
notif.defaults = Notification.DEFAULT_ALL;
nm.notify(NOTIF_ID, notif);
String tempSMS = msgs[i].getOriginatingAddress();
Intent pass = new Intent();
Bundle bundlePass = new Bundle();
bundlePass.putString("key", tempSMS);
pass.putExtras(bundlePass);
}
}
}
And this is the PART OF SMSReceiver that i used for passing a String via Bundle and Intent
String tempSMS = msgs[i].getOriginatingAddress();
Intent pass = new Intent();
Bundle bundlePass = new Bundle();
bundlePass.putString("key", tempSMS);
pass.putExtras(bundlePass);
Now, this is the complete SMSReply Class, the class that i want to get a phone number(String) from SMSReceiver Class(so user don’t have to type it again)
public class SMSReply extends Activity implements OnClickListener {
EditText etPhone, etMessage;
Button bSend;
String phone, message;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reply);
etPhone = (EditText) findViewById(R.id.etPhone);
etMessage = (EditText) findViewById(R.id.etMessage);
bSend = (Button) findViewById(R.id.bSend);
bSend.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent get = new Intent();
Bundle bundleGet = get.getExtras();
phone = bundleGet.getString("answer");
etPhone.setText(phone);
phone = etPhone.getText().toString();
message = etMessage.getText().toString();
if (phone.length() > 0 && message.length() > 0) {
sendSms(phone, message);
} else {
Toast.makeText(getBaseContext(),
"Please enter both phone number and message",
Toast.LENGTH_SHORT).show();
}
}
private void sendSms(String phone2, String message2) {
// TODO Auto-generated method stub
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic Failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio Off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS Delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not Delivered",
Toast.LENGTH_SHORT).show();
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phone, null, message, sentPI, deliveredPI);
}
And this is the PART OF SMSReply class, where i put some code’s for taking a String from SMSReceiver and set that String in my EditText 😀
Intent get = new Intent();
Bundle bundleGet = get.getExtras();
phone = bundleGet.getString("answer");
etPhone.setText(phone);
phone = etPhone.getText().toString();
message = etMessage.getText().toString();
Thats All!
Please help me, and thank you very much! 😀
PS : Sory for the long post, and sory if my English is not good, English is not my native languange 😀
While setting the extra arguments in bundle, you put that bundle in pass Intent and you are doing nothing of that pass Intent. Actually, you should pass these bundle into the intent which you are passing for Notification, i.e. in intent Intent.
intent.putExtras(bundlePass);.Similarly, while reading the argument from bundle, you are accessing it from a new intent which is not the actual one which comes, So use
Bundle bundleGet = getIntent().getExtras();