0

1.你好,我已经为计划消息创建了服务,但问题是我在我的服务方法中设置了循环,但它发送的消息是列表中设置的所有消息。

2.我想设置循环,发送列表上的消息将至少发送一次。

3.已经发送的消息不再发送。

//这是我的代码。 //我的服务类

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.widget.Toast;
public class SchedulerMsgService extends Service {
    private static final String TAG = "MyService";
    private DatabaseHelper mDbHelper;
    private ArrayList<schedulerDetails> myschedule = new ArrayList<schedulerDetails>();
    private String phoneNo, message, sDate, curTime;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
    }
    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }
    @Override
    public void onStart(Intent intent, int startId) {
        Log.d(TAG, "oNsTART IS rEADY ");
        mDbHelper = new DatabaseHelper(this);
        final List<schedulerDetails> ScheduleList = mDbHelper.selectAllNumbers();
        for (int j = 0; j < ScheduleList.size(); j++) {
            myschedule.add(ScheduleList.get(j));
            phoneNo = myschedule.get(j).num;
            message = myschedule.get(j).textMessage;
            sDate = myschedule.get(j).date;
            curTime = myschedule.get(j).time;

            sendSMS(phoneNo, message, sDate, curTime);
        }


        Toast.makeText(this, "condition Matched", Toast.LENGTH_LONG).show();

        // Toast.makeText(this, "condition Not Matched",
        // Toast.LENGTH_LONG).show();
    }
    public void sendSMS(String phoneNo, String message, String sDate,
            String curTime) {
        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);
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS Sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic Failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "Null Service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio Off",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }, new IntentFilter(SENT));
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS Delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not Delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }, new IntentFilter(DELIVERED));
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);
    }
}
4

1 回答 1

0

如果消息已发送,则从数据库中删除该号码或在数据库中添加消息已发送的一些信息。然后mDbHelper.selectAllNumbers()可以只返回需要接收消息的号码。

于 2012-04-25T09:03:03.223 回答