1.こんにちは、スケジュールメッセージのサービスを作成しましたが、問題はサービスメソッドでループを設定したことですが、リストに設定されているすべてのメッセージをメッセージに送信します。
2.ループを設定したいのですが、リストのメッセージを送信すると、少なくとも1回送信されます。
3.送信済みのメッセージは再送信されません。
//これが私のコードです。 //MyService クラス
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);
}
}