在我的应用程序中,我创建了一个编辑文本视图和三个按钮。当用户点击“添加更多联系人”按钮时,它将添加另一个编辑文本视图,当用户点击“添加联系人”时,这将带他到电话簿的联系人,从那里他可以选择他想要发送的联系人短信。我已经编写了所有这些程序,但我不知道如何在没有用户确认的情况下自动向所有用户发送消息,只需按下“发送消息”按钮。我试过SMSManager但它已经折旧了。请建议我。
public class MainActivity extends Activity {
private Button btn_cntct;
public int REQUESTCODE = 1;
private LinearLayout layoutLinear;
private Button btn_addmore_cntct;
private int id = 1;
Button b_alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// defining button elements for picking contacts from phone-book
btn_cntct = (Button) findViewById(R.id.bpickperson);
btn_cntct.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// using Intent for fetching contacts from phone-book
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, REQUESTCODE);
}
});
// defining button and edit-text values for adding mutli edit-texts
// views
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(OnClick());
EditText editview = new EditText(this);
editview.setText("Add more");
}
// implementing OnClickListener OnClick() method for "btn_addmore_cntct"
// button
private OnClickListener OnClick() {
// TODO Auto-generated method stub
// changing return type "null" to "new OnClickListner"
return new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// setting id for edit-text views
tab.setId(id);
// and increment
id++;
// adding (0) to display edit-text view on the top
layoutLinear.addView(tab, 0);
tab.requestFocus();
// for sending sms
b_alert = (Button) findViewById(R.id.balert);
b_alert.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String smsNumber = tab.getText().toString();
String smsText = "I am in danger";
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", smsText);
startActivity(intent);
}
});
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// calling onActivityResult when contacts has been selected from the
// phone-book, giving back the REQUESTCODE i started it with, the RC it
// returned with
// any additional data from it.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri uri = data.getData();
Log.i("data", uri.toString());
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver()
.query(uri,
new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
String name = c.getString(0);
String number = c.getString(1);
int type = c.getInt(2);
showSelectedNumber(name, number, type);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
// defining showSelectedNummber to display selected contact from phone-book
// to Edit-Text View
public void showSelectedNumber(String name, String number, int type) {
if (layoutLinear == null) {
Log.i("layoutLinear is null", "null");
} else {
Log.i("layoutLinear is not null", "not null");
}
EditText userNumber = (EditText) layoutLinear.getChildAt(0);
if (userNumber == null) {
Log.i("edittext is null", "null");
} else {
Log.i("edittext is not null", "not null");
}
String typeNumber = (String) ContactsContract.CommonDataKinds.Phone
.getTypeLabel(getResources(), type, "");
userNumber.setText(name + ": " + number + " " + typeNumber);
}
}
答案 0 :(得分:0)
您需要发送数字的this
SmsManager.getDefault().sendTextMessage("NUMBER",
null, "TEXT_TO_SEND", null, null);