我需要在我的应用程序中发送带有数据库中存储的联系号码的短信。
目前我在那里:
// Gets the context so it can be used later
public ButtonAdapter(Context c, ArrayList<Message> list) {
mContext = c;
this.list = list;
}
// Total number of things contained within the adapter
public int getCount() {
return list.size();
}
// Require for structure, not really used in my code.
public Object getItem(int position) {
return null;
}
// Require for structure, not really used in my code. Can
// be used to get the id of an item in the adapter for
// manual control.
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
Button btn;
if (convertView == null) {
// if it's not recycled, initialize some attributes
btn = new Button(mContext);
btn.setLayoutParams(new GridView.LayoutParams(200, 80));
btn.setPadding(8, 8, 8, 8);
}
else {
btn = (Button) convertView;
}
btn.setText(list.get(position).getTitre());
if (btn != null) {
btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (ActivityCompat.checkSelfPermission(v.getContext(), Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale((Activity) v.getContext(), Manifest.permission.SEND_SMS)) {
explain();
} else {
//demander l'autorisation
}
} else {
ArrayList<Contact> numero;
String message = list.get(position).getContenu();
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(numero, null, parts, null, null);
Toast.makeText(v.getContext(), "Vous venez d'envoyer le SMS : " + message, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
if (btn != null) {
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("NON CONFIRME", list.get(position).getContenu());
}
});
}
// filenames is an array of strings
btn.setTextColor(Color.WHITE);
btn.setBackgroundResource(R.drawable.button);
btn.setId(position);
return btn;
}
private void askForPermission() {
}
private void explain() {
askForPermission();
}
我希望当长长的陈词滥调时,应用程序发送一个短信已经存储在我的数据库中。
ArrayList<Contact> numero;
String message = list.get(position).getContenu();
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(numero, null, parts, null, null);
Toast.makeText(v.getContext(), "Vous venez d'envoyer le SMS : " + message, Toast.LENGTH_SHORT).show();
我在数据库中的功能:
public static ArrayList<Contact> fetchAllNumero()
{
ArrayList<Contact> mArrayList = new ArrayList<>();
Cursor mCursor = bdd.query(TABLE_CONTACT, new String[]{COL_ID, COL_NUMERO}, null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
mCursor.moveToFirst();
while (!mCursor.isAfterLast())
{
Contact contact = new Contact();
contact.setId(mCursor.getInt(mCursor.getColumnIndex(COL_ID)));
contact.setNumero(mCursor.getString(mCursor.getColumnIndex(COL_NUMERO)));
mArrayList.add(contact);
mCursor.moveToNext();
}
}
return mArrayList;`
}
我不知道在哪里显示号码并发送完整短信。
感谢您的帮助。