2017-01-19 5 views
0

Android 프로그래밍을 처음 접했고 다음과 같이 작동하는 응용 프로그램을 만들고 있습니다. SMS에서 "-COMMAND fileName"형식의 "명령"을 사용하여 입력을받은 후 메시지 내용을 읽고 특정 멀티미디어를 실행합니다 앱의 다른 활동에서의 작업.동일한 파일에서 다른 시간에 작동하는 두 개의 메서드를 동일한 Activity에서 어떻게 호출 할 수 있습니까?

동일한 파일 (예 : "-SHOTPHOTO photo1 -SENDPHOTO photo1")에서 작업하는 명령이 하나의 SMS에있을 때 App은이 두 가지 방법을 호출하지만 첫 번째 방법 만 올바르게 실행됩니다. 사진이 아직 촬영되지 않았으므로 다른 하나는 오류를 반환합니다.

// onCreate of the new Activity 

// I received an Intent from an SMS Broadcast Receiver 
// The commands and file names are saved in order in command[nCommands] and files[nFiles], nCommands and nFiles are integer and represents the number of commands/file names 

for (int i = 0; i < nCommands; i++) { 
    switch (command[i]) { 
     case "-SHOTPHOTO": 
      // finds the correct file name of this command 
      shotphoto(files[j]); 
      break; 
     case "-SENDPHOTO": 
      // finds the correct file name of this command 
      sendphoto(files[j]); 
      break; 
      } 
} 

// end of onCreate 

public void shotphoto (String photoName) { 
    // standard method to take photos: calls the default camera app with startActivity 
    // takes photo and then renames it to photoName 
    // photo saved in the ExternalStorage, in my app folder 
} 

public void sendphoto(String photoName) { 
    // standard method to send email + photo: calls the default mail app with startActivity 
    // gets the photo from my app's folder in the ExternalStorage 
    // the photo is sent to the sender's mail address, which is already known 
} 

두 명령은 두 개의 서로 다른 메시지 또는 때 어떤 문제가 없습니다있을 때, 예를 들어, 다른 메시지에서 메시지와 -SHOTPHOTO의 '사진 -SENDPHOTO의 사진 1에서 -SHOTPHOTO의 사진 1. 나는 읽기 및 정확성 제어 프로세스를 테스트했으며 문제가 없다. 그래서 내 문제는 두 가지 방법은 동시에 실행되고 sendphoto()는 아직 촬영되지 않았기 때문에 사진을 찾지 못한다고 생각합니다.

두 가지 방법을 만드는 방법에 대한 아이디어가 있으므로 첫 번째 명령은 항상 두 번째 명령 전에 실행되고 두 번째 명령은 첫 번째 명령이 완료 될 때까지 대기하게됩니다.

"-HOTNSEND photoName"명령을 추가하지 않습니다. 원하는 것이 아니기 때문입니다. shotphoto()의 끝 부분에 sendphoto()를 추가해도 실제로 사진을 보내지 않고 사진을 찍을 수는 없습니다.

여기에 작성한 코드는 실제 코드의 기본적인 예일뿐입니다. 명확하지 않거나 중요한 것이없는 경우 알려주세요.

+0

대신에 모든 명령으로 대기열을 만드십시오. 첫 번째 작업이 끝나면 다음 명령을 실행하십시오. – X3Btel

+0

답변 해 주셔서 감사합니다. 최대한 빨리 시도하겠습니다! –

답변

0

@ X3Btel의 조언을 따라 대기열을 만들고 내 문제를 해결했습니다. 내가 한 방법을 알고 싶은 분들을 위해 , 여기에 코드입니다 :

private Queue<String> qCommands; // global 

public void onCreate(...){ 
    qCommands = new ArrayDeque<>(); 
    // read SMS' text 
    qCommands.add(command[i]);  // everytime I read a command 
    // same as before but without for(i->nCommands) and switch command[i] 
    nextCommand(qCommands.poll()); 
} 

public void nextCommand(String command){ 
    if(command != null){ 
     switch (command) { 
      case "-SHOTPHOTO": 
      // finds the correct file name of this command 
      shotphoto(files[j]); 
      break; 
     case "-SENDPHOTO": 
      // finds the correct file name of this command 
      sendphoto(files[j]); 
      break; 
     } 
    } else { 
     // the queue is empty so no more commands 
} 

// both shotphoto() and sendphoto() start an Activity for result so when they have finished onActivityResult will be called 
public void onActivityResult(...){ 
    if(resultcode == SHOTPHOTO) 
     nextCommand(qCommands.poll()); 
    if(resultcode == SENDPHOTO) 
     nextCommand(qCommands.poll()); 
} 

응용 작품과 예전처럼 오류를 반환하지 않습니다. 답장을 보내 주셔서 다시 한번 감사드립니다. @ X3Btel!