Start an intent when another intent has finished

I have some troubles in my Android app. I need to start an intent when another intent has finished. The problem is that both intents are launched at the same time (i don't know why). Here is my code:

In this dialog i ask if the user wants to take a photo before send an email (and of course i want to send the email with the photo). Ok?

private Dialog comprobarFoto() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this); 

        builder.setMessage("¿Desea adjuntar una foto con este mensaje de su posición actual?");
        builder.setPositiveButton("Si", new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                hacerFoto();
                enviarMail(true);
                dialog.cancel();
            }
        });
        builder.setNegativeButton("No", new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                enviarMail(false);
                dialog.cancel();
            }
        }); 
        return builder.create();    
    }

Here is the method where I create the email

private void enviarMail(boolean foto) {
        if(locMail!=null) {
            Intent i = new Intent(Intent.ACTION_SEND);  
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_SUBJECT,"Quiero que sepas donde estoy!"); 
            if(foto && fotoPath != null && fotoPath.length()>0) {
                i.putExtra(Intent.EXTRA_STREAM, Uri.parse(fotoPath));
            }
            i.putExtra(Intent.EXTRA_TEXT,"Hola!n Estoy en la dirección:" +
                    "n" + localizarDireccion() +
                    "nn(lat= " + dfmail.format(locMail.getLatitude()) + ")" +
                    "n(long=" + dfmail.format(locMail.getLongitude()) + ") " +
                    "n n[Enviado desde GeoLocation]" );  
            startActivity(i);
        }else{
            dialogError("No se puede encontrar una localización").show();
        }
    }

And this is the method where I take the photo:

private void hacerFoto() {  
        //El gran quebradero de cabeza aquí fue que no le daba permisos a la aplicación para escribir en la tarjeta. En el manifest se puede comprobar esta directiva
        File carpeta = new File(Environment.getExternalStorageDirectory () + "/GeoLocation/images"); 
        if(!carpeta.exists()){
            carpeta.mkdirs();
        }
        fotoPath = Environment.getExternalStorageDirectory() + "/GeoLocation/images/GLPict"+cogerFecha()+".jpg";
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri output = Uri.fromFile(new File(fotoPath));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
        startActivityForResult(intent, 1);
    }

The problem is that the intent of sendMail (enviarMail) doesn't wait to the first method takePhoto (hacerFoto) has finished. Is there some method that let me say to the sendMail intent to wait until the method takePhoto has finished?

Thanks for all and SORRY FOR MY ENGLISH!!!


Start first intent like startActivityForResult(intent, requestCode) and when this will return onActivityResult gets called, and from there you can start the second intent.

edit :: do it like this ::

private Dialog comprobarFoto() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    builder.setMessage("¿Desea adjuntar una foto con este mensaje de su posición actual?");
    builder.setPositiveButton("Si", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            hacerFoto();
      ///////////// do not call enviarMail(true) from here, call it from onActivityResult, see below //////////////////
            dialog.cancel();
        }
    });
    builder.setNegativeButton("No", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            enviarMail(false);
            dialog.cancel();
        }
    }); 
    return builder.create();    
}

@Override
protected void onActivityResult(int reqCode,int resultCode,Intent intent)
{
    if(resultCode == Activity.RESULT_OK)
    {
        if( reqCode == 1)
            enviarMail(true); //////// call from here ///////   
    }
}

你不能使用onFinish()或onStop()来启动下一个意图吗?

链接地址: http://www.djcxy.com/p/23262.html

上一篇: 直接发送电子邮件没有意图

下一篇: 当另一个意图完成时开始一个意图