android向多个号码发送多条消息是行不通的

时间:2014-12-22 08:37:49

标签: android sms

我想向多个号码发送多条消息(例如将座位号码发送给在剧院预留座位的人员)作为我项目的一部分。我已经成功地从数据库中检索了消息和电话号码,并将其显示在列表视图,这是我的代码。

SQLiteDatabase db = dbhelper.getWritableDatabase();
        Cursor mCursor=db.rawQuery("SELECT * FROM contacts WHERE date='"+label+"'", null);

        userId.clear();
        user_phoneNumber.clear();

        if (mCursor.moveToFirst()) {
            do {
                userId.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_ID)));
                user_phoneNumber.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_PHONENUMBER)));


    } while (mCursor.moveToNext());
        }
        DisplayAdapter disadpt = new DisplayAdapter(home.this,userId, user_phoneNumber);
        lv.setAdapter(disadpt);
        disadpt.notifyDataSetChanged();

        mCursor.close();

这里当我运行程序时,它只发送到我在那里提到的第一个号码,并且所有消息都将转到该号码。但是我希望将第一条消息发送到第一个号码,将第二条消息发送到第二个号码,就像那样...... 。任何人都可以帮我找鳕鱼。

  String receipentsMessage[] = {userId.toString()};


    //this id is used as their seat number
        String receipentsNumber[] = {user_phoneNumber.toString()};

        for (int i = 0; i < receipentsNumber.length; i++)
        for (int j = 0; j < receipentsMessage.length; j++)
        {

        try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(receipentsNumber[i], null,receipentsMessage[j], null,
                            null);
                Toast.makeText(getApplicationContext(), "SMS Sent to" + " " + receipentsNumber[i]+receipentsMessage[j], Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }

1 个答案:

答案 0 :(得分:0)

让我们看看你的代码:

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(receipentsNumber[i], null,receipentsMessage[i], null,
                    null);
        Toast.makeText(getApplicationContext(), "SMS Sent to" + " " + receipentsNumber[i]+receipentsMessage[i], Toast.LENGTH_LONG).show();

问题是您没有使用j索引。建议代码:

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(receipentsNumber[i], null,receipentsMessage[j], null,
                    null);
        Toast.makeText(getApplicationContext(), "SMS Sent to" + " " + receipentsNumber[i]+receipentsMessage[j], Toast.LENGTH_LONG).show();

修改

    for (int i = 0; i < user_phoneNumber.size(); i++)

    {

    try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(user_phoneNumber.get(i), null,userId.get(i), null,
                        null);
            Toast.makeText(getApplicationContext(), "SMS Sent to" + " " + user_phoneNumber.get(i)+userId.get(i),Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    }