向SQLite数据库中的每个联系人发送短信

时间:2017-07-27 23:58:18

标签: android sqlite

我有一个SQLite表,每个表都有不同的电话号码和不同的消息。我想获取每一行的ID,以便我的IntentService可以将唯一的SMS发送到每个号码。 这就是我所做的:

public class NotificationService extends IntentService {
public NotificationService() {
    super(NotificationService.class.getSimpleName());
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        String number = extras.getString(String.valueOf(MessageContract.MessageEntry.COLUMN_PHONE_NUMBER));
        String message = extras.getString(String.valueOf(MessageContract.MessageEntry.COLUMN_CODE));
        sendMessage(number, message);
    }
}

private void sendMessage(String number, String message) {
    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);

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(number, null, message, sentPI, deliveredPI);

}

这是我的合同类

public final class MessageContract {

private MessageContract() {}

public static class MessageEntry implements BaseColumns {
    public static final String TABLE_NAME = "message";
    public static final String COLUMN_CODE = "code";
    public static final String COLUMN_PHONE_NUMBER = "phoneNumber";
}
}

请帮忙解决这个问题。

1 个答案:

答案 0 :(得分:0)

试试这个

Cursor cursor = database.getSavedNumbers();
if (cursor.moveToFirst())
  {
do 
  { 
  String message = cursor.getString(0);//Column Message should be on 0th position 
  String number = cursor.getString(1);//Column Number should be on 1st Position
  SmsManager smsManager = SmsManager.getDefault();
  smsManager.sendTextMessage(number, null, message, null, null);
}while(cursor.hasNext());
}

cursor.close();