Java 我如何在同一个活动中调用两个方法,这两个方法将在同一个文件上但在不同的时间工作?
我是Android编程新手,我正在制作一个类似这样的应用程序:在接收到来自SMS的带有一些“命令”(以命令文件名的形式)的输入后,它读取消息的内容,并在应用程序的另一个活动中执行某些多媒体操作 问题是,当一条短信中的命令在同一个文件上工作时(例如“-SHOTPHOTO photo1-SENDPHOTO photo1”),应用程序会调用这两种方法,但只有第一种方法正确执行;另一个返回一个错误,因为照片尚未拍摄Java 我如何在同一个活动中调用两个方法,这两个方法将在同一个文件上但在不同的时间工作?,java,android,email,call,photo,Java,Android,Email,Call,Photo,我是Android编程新手,我正在制作一个类似这样的应用程序:在接收到来自SMS的带有一些“命令”(以命令文件名的形式)的输入后,它读取消息的内容,并在应用程序的另一个活动中执行某些多媒体操作 问题是,当一条短信中的命令在同一个文件上工作时(例如“-SHOTPHOTO photo1-SENDPHOTO photo1”),应用程序会调用这两种方法,但只有第一种方法正确执行;另一个返回一个错误,因为照片尚未拍摄 // onCreate of the new Activity // I receiv
// 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
}
//一旦创建新活动
//我从短信广播接收器收到了一份意向书
//命令和文件名按命令[nCommands]和文件[nFiles]的顺序保存,nCommands和nFiles为整数,表示命令/文件名的数量
对于(int i=0;i
当这两个命令位于两个不同的消息中,或者当消息中有-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());
}
专用队列qCommands;//全球的
创建时的公共无效(…){
qCommands=new ArrayDeque();
//阅读短信
qCommands.add(command[i]);//每次读取命令时
//与前面相同,但没有for(i->nCommands)和switch命令[i]
nextCommand(qCommands.poll());
}
public void nextCommand(String命令){
if(命令!=null){
开关(命令){
案例“-照片”:
//查找此命令的正确文件名
照片(档案[j]);
打破
案例“-SENDPHOTO”:
//查找此命令的正确文件名
sendphoto(文件[j]);
打破
}
}否则{
//队列为空,因此没有更多命令
}
//shotphoto()和sendphoto()都会为result启动一个活动,因此当它们完成时,将调用ActivityResult
在ActivityResult上的公共无效(…){
if(resultcode==SHOTPHOTO)
nextCommand(qCommands.poll());
if(resultcode==SENDPHOTO)
nextCommand(qCommands.poll());
}
该应用程序工作正常,不会像以前那样返回错误。
再次感谢您,@X3Btel,谢谢您的回答!请将所有命令排成队列。第一个命令完成后执行下一个命令谢谢您的回答,我会尽快尝试!