2012-09-14 3 views
0

개발중인 코드 (android application)는 SMS를 받고 특정 번호의 SMS 인 경우 사전 정의 된 SMS를 반환합니다. 이 모든 것은 정상적으로 작동하지만 회신이 사실적으로 보이도록 타이머를 추가하려고합니다. 약간의 연구를 통해 아래 코드를 시도하여 코드를 지연시켜 메시지가 30 초 지연 후에 전송되도록했습니다.TimerTask를 사용하여 SMS 전송 지연

나는 timerSlt라는 새로운 타이머를 정의하고, 30 초의 지연과 맨 위에 tt1이라는 TimerTask를 정의한 다음 onSelect 메서드를 호출하기 직전에 onReceive() 메서드를 호출하기 전에 timer() 메서드를 호출합니다. 시간제 노동자.

그러나 타이머는 절대로 멈추지 않으며 메시지는 보내지 않습니다. 결국 몇 가지 응용 프로그램이 충돌합니다 내가 뭘 잘못하고 있는지 알 수 있습니까?

static Timer timer1 = new Timer(); 
static long delay = 30000; 
static TimerTask tt1; 

     private BroadcastReceiver intentReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     //---gather up all the necessary user input--- 
     prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
     String textMessage = (String) prefs.getString(NAME_KEY, ""); 
     final Button btn2 = (Button)findViewById(R.id.btnContacts); 
     String phoneNumber = (String) btn2.getText(); 
     String Sender = (String) intent.getExtras().getString("Sender"); 

     if(Sender.equals(phoneNumber)) 
     { 
      timer(); 
      sendSMS(phoneNumber, textMessage); 
     } 
     } 
    } 
    }; 

     //---holds the delay for realistic reply time--- 
public static void timer() 
{ 
    timer1.schedule(tt1 ,0,delay);  
} 




//---sends an SMS message to another device--- 
public void sendSMS(String phoneNumber, String message) 
{  
    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(phoneNumber, null, message, null, null); 
} 

편집 : 지금 텍스트를 보내기에 일시 정지 handler.postDelayed을 사용하고 있습니다. 이 훨씬 더 잘 작동 handler.postDelayed 좋은 초보자 자원이 여기

: http://www.appaholics.in/running-a-task-with-a-delay/

답변

0
what I am doing wrong? 

를 이것은 당신이 잘못된 곳이다

timer1.schedule(tt1 ,0,delay);

위의 진술은 cre SMS 보내기 작업을 반복하기 위해 매 30 초마다 일정을 잡았습니다.

나는

timer1.schedule(tt1 ,100); 

이제 위의 코드가 보낸 한 시간에 SMS를 답장을 것입니다, 당신은 다음과 같이 코드를 변경해야 있도록, 보낸 사람에게 한 번만를 SMS 회신 할 필요가 생각 100 밀리 초 후.