2012-04-25 36 views
0

1.hello,我創建了Schedule消息的服務,但問題是我已經在我的服務方法中設置了循環,但它將消息發送到列表上的所有消息。多次從列表中調用數據的Android服務

2.我想設置的循環一樣,在列表發送消息將至少那些時間發送。

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); 
    } 
} 

回答

0

如果已發送的郵件,然後從數據庫中刪除數或添加該消息已經被髮​​送的數據庫的一些信息。然後mDbHelper.selectAllNumbers()只能返回需要接收消息的號碼。

+0

先生,我不明白。用代碼解釋。 PLZ先生.. – 2012-04-25 09:51:03

+0

先生我已嘗試發送消息刪除,但問題是,現在在列表中只發送列表中的最後一組記錄消息。 我想發送所有尚未發送時間的消息 例如,如果兩個消息在列表中設置的時間已經設置爲下午2點,而另一個設置的時間爲2-03點我想要發送列表中的所有消息,但在我的情況下,它只發送最後一個消息集。 – 2012-04-25 10:02:42

+0

對不起,我不明白你必須發送/不發送消息的條件 - 更新你的問題,並嘗試用例子來描述。 - 但是一旦'ScheduleList'包含列表並且沒有信息,如果該消息必須被髮送,那麼你不能阻止它發送你發佈的代碼。您必須更改生成此列表的邏輯以適合您的需求。 – zapl 2012-04-25 10:08:39