2011-12-08 56 views
0

嘿大家在Stackoverflow。Android短信 - 在Button.click发生两次后发送消息

我得到了这个令人难以置信的烦人的问题。我尝试构建一个SMS应用程序,它将使用AlarmManager在特定时间发送SMS。够搞笑(因为它似乎是最难的)在特定时间发送短信完美的作品, 但什么刺激(或杀死)我是,它可以只发送短信后,我已经点击按钮两次?!

任何人有一个想法,因为我很快就会想到这里,一直在尝试几个小时。

下面的代码是为Button.click事件的代码:

public void onClick(View arg0) 
      { 
       // TODO Auto-generated method stub 
       //txtClear(); 

       try { 

        Intent myIntent = new Intent(SMSAlarm.this, MyAlarmService.class); 
        smsNumber = txtPhoneNo.getText().toString(); 
        smsText = txtMessage.getText().toString(); 

//     if(smsNumber.length()<=0 || smsText.length()<=0) 
//     { 
//      Toast.makeText(getBaseContext(), 
//        "Please enter both phone number and message", 
//        Toast.LENGTH_SHORT).show(); 
//     } 
//     else 
//     { 

        TimePicker timePickerAlarm = (TimePicker) findViewById(R.id.timePickerAlarm); 
        DatePicker datePick = (DatePicker) findViewById(R.id.datePickerAlarm); 

        Calendar calendar = calendarInformation(timePickerAlarm, 
          datePick); 

        Calendar today = Calendar.getInstance(); 

        //Check if chosen date is less than the current date and time 
        //IF True, inform the user that the selected date has passed 
        //ELSE, start the service for sending SMS 

//     if(calendar.getTime().getTime() <= today.getTime().getTime()) 
//     { 
//      Toast.makeText(getBaseContext(), "Date has passed!\nPlease choose a new date", Toast.LENGTH_LONG).show(); 
//     } 
//     else 
//     { 

         Toast.makeText(SMSAlarm.this, 
           "Start Alarm with \n" + 
           "smsNumber = " + smsNumber + "\n" + 
           "smsText = " + smsText, 
           Toast.LENGTH_LONG).show(); 

         Bundle bundle = new Bundle(); 
         bundle.putCharSequence("extraSmsNumber", smsNumber); 
         bundle.putCharSequence("extraSmsText", smsText); 
         myIntent.putExtras(bundle); 
         requestCode+= 1; 
         //Temporary solution. Added to make the requestCode unique, to be able to send more than one SMS in a row with different text and to a different number. 
         pendingIntent = PendingIntent.getService(SMSAlarm.this, requestCode, myIntent, PendingIntent.FLAG_ONE_SHOT); //PendingIntent.FLAG_ONE_SHOT 
         //pendingIntent = PendingIntent.getService(SMSAlarm.this, 0, myIntent, 0); 
         //} 
        //} 
        txtClear(); 
       }          
       catch (Exception e) 
       { 
        // TODO Auto-generated catch block 
        Toast.makeText(getBaseContext(), "Error sending SMS " + e.getMessage(), Toast.LENGTH_LONG).show(); 
        e.printStackTrace(); 
        txtClear(); 
       } 
       } 

这里是MyAlarmService类:

public class MyAlarmService extends Service 
{ 
    String smsNumberToSend, smsTextToSend; 
    @Override 
    public void onCreate() 
    { 
     //TODO Auto-generated method stub 
     //Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show(); 
     return null; 
    } 

    @Override 
    public void onDestroy() 
    { 
     //TODO Auto-generated method stub 
     super.onDestroy(); 
     Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     //TODO Auto-generated method stub 
     try { 
      super.onStart(intent, startId);  

      Bundle bundle = intent.getExtras(); 
      smsNumberToSend = (String) bundle.getCharSequence("extraSmsNumber"); 
      smsTextToSend = (String) bundle.getCharSequence("extraSmsText"); 

      Toast.makeText(this, "Sending Message...", Toast.LENGTH_LONG).show(); 
      Toast.makeText(this, 
        "Number = " + smsNumberToSend + "\n" + 
        "Message = " + smsTextToSend,    
        Toast.LENGTH_LONG).show(); 

      sendSMS(smsNumberToSend, smsTextToSend); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    private void sendSMS(String phoneNumber, String message) { 
     // TODO Auto-generated method stub 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage(phoneNumber, null, message, null, null); 
    } 

    @Override 
    public boolean onUnbind(Intent intent) 
    { 
     //TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();   
     return super.onUnbind(intent);  
    } 
} 

任何帮助深表感谢

+0

何时/在哪里启动MyAlarmService? – Peter

+0

pendingIntent = PendingIntent.getService(SMSAlarm.this, requestCode,myIntent,PendingIntent.FLAG_ONE_SHOT); 而这种情况发生在Button.click事件中,但它只发生在我第二次单击按钮时:S – Anton

回答

0
  pendingIntent = PendingIntent.getService(SMSAlarm.this, requestCode, myIntent, 
           PendingIntent.FLAG_ONE_SHOT); 

将PendingIntent.FLAG_ONE_SHOT更改为myIntent.FLAG_ACTIVITY_NEW_TASK