我怎样才能在同一个 Activity 中调用两个方法,它们将在同一个文件上工作,但在不同的时间?

How can I call two method in the same Activity that will work on the same file but in different times?

我是 Android 编程的新手,我正在制作一个这样工作的应用程序:在收到来自 SMS 的一些 "commands" 的输入后(格式为 -COMMAND fileName),它读取消息的内容并在应用程序的另一个 Activity 中执行某些多媒体操作。

问题是,当一条 SMS 中有命令处理同一个文件时(例如“-SHOTPHOTO photo1 -SENDPHOTO photo1”),应用会调用两种方法来执行此操作,但只有第一个正确执行;另一个 returns 出错,因为照片还没有拍完。

// 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 photo1 和另一个消息中有 -SHOTPHOTO photo2 -SENDPHOTO photo1 时,我没有任何问题。我测试了读取和正确性控制过程,没有问题。 所以我觉得我的问题是这两个方法是同时执行的,sendphoto()没有找到照片,因为还没有拍到

关于如何使两个方法同步的一些想法,以便第一个命令总是在第二个命令之前执行,第二个命令等待第一个完成?

我不会添加命令“-SHOTNSEND photoName”,因为这不是我想要的。将 sendphoto() 添加到 shotphoto() 的末尾将不允许我在不实际发送照片的情况下拍照。

我在这里写的代码只是真实代码的一个非常基本的例子,如果有不清楚的地方或者遗漏了一些非常重要的东西,请告诉我。

我听从了@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());
}

该应用可以运行,不会像以前那样出现 return 错误。 再次感谢@X3Btel 的回答!