我想从一个活动传递一个字符串变量到另一个活动,但不是为我工作,我想从主要业务的字符串发送到sendsms活动发送字符串应该设置在短信部分。Android的传递字符串从一个活动到另一个活动是行不通的,
从主要活动主要activity.java
@Override
public void onClick(View v) {
StringBuffer sb = new StringBuffer();
// Retrive Data from list
for (Application bean : items) {
if (bean.isSelected()) {
sb.append(Html.fromHtml(bean.getContent()));
sb.append(",");
}
}
showAlertView(sb.toString().trim());
}
@SuppressWarnings("deprecation")
private void showAlertView(String str) {
AlertDialog alert = new AlertDialog.Builder(this).create();
final String strContactList = str.substring(0, str.length() - 1);
if (TextUtils.isEmpty(str)) {
alert.setTitle("Not Selected");
alert.setMessage("No One is Seleceted!!!");
} else {
// Remove , end of the name
alert.setTitle("Selected");
alert.setMessage(strContactList);
}
alert.setButton("sms", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//sendSMS();
/*Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", strContactList);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);*/
Intent intent1=new Intent(MainActivity.this,SendSMSActivity.class);
intent1.putExtra("firstkeyName", strContactList);
startActivity(intent1);
}
});
发送字符串sendsmsactivity.java
sendsmsactivity.java
public class SendSMSActivity extends Activity {
Button buttonSend;
EditText textPhoneNo;
EditText textSMS;
String sms;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
buttonSend = (Button) findViewById(R.id.buttonSend);
textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
textSMS = (EditText) findViewById(R.id.editTextSMS);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
sms = extras.getString("firstkeyName");
}
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phoneNo = textPhoneNo.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}
在sendsmsactivity,我想要得到的字符串从主要活动,它必须设置为发送短信活动的短信主体。我想这样做,但现在我的代码不工作,它没有得到从主到字符串ndsmsactivity。
短信= extras.getString( “firstKeyName”); shoudl be sms = extras.getString(“firstkeyName”);关键字应该匹配。你的密钥K为你的密钥在sendingmsactivity.java中,而在mainactivity中你有intent1.putExtra(“firstkeyName”,strContactList); – Raghunandan
将您的密钥从putExtra()复制并粘贴到getString(); – NaserShaikh
老兄,我纠正这一个,尽管它没有提取,但我把短信=额外....这样,并在这里smsManager.sendTextMessage(phoneNo,null,sms,null,null);我放这样,这是正确的想要添加任何其他代码@Raghunandan –