0

我有一个通过电子邮件发送一些数据的 Android 应用程序。我还有一个按钮可以触发一个清除相同数据的 AlertDialog 确认框。

该应用程序的正常流程是:将数据填充到pdf文件中->发送带有pdf附件的电子邮件->按下按钮并重复清除数据。

我的问题是:用户是否有可能在发送电子邮件之前清除他们的数据(所以数据都是空白的)?我不希望我的用户因为错误而发送空白电子邮件。我问这个问题的原因是因为我在电子邮件活动被触发后首先清除了数据(这是禁止的)并且正在发送空白数据。我已经尝试寻找一种在电子邮件活动上使用 StartActivityForResult() 的方法,这样我就可以在活动完成时销毁数据,但看起来这是不可能的。我认为使用确认框进行操作是安全的,因为应该完成电子邮件活动,但我只是想绝对确定这是安全的。

4

1 回答 1

0

Android devices can behave very different, one from another. You could have slowness in your mail activity, or you could have your device to suddenly decide to clean up memory and make every thing slow slow slow for a moment. If you are relying on timing in hoping that some other process or thread is completed before you delete something, you are playing with fire.

I think a better approach would be to start your e-mail activity for result, like CapDroid did in this answer:

Send Mail:

int EMAIL = 101;

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{});
emailIntent.putExtra(android.content.Intent.EXTRA_BCC,new String[]{});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
startActivityForResult(emailIntent,EMAIL);

Sending Result:

   protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            // TODO Auto-generated method stub
            if(requestCode==EMAIL)
            {
                if(requestCode==EMAIL && resultCode==Activity.RESULT_OK)    
                {
                                if(myFile.exists())
                                    myFile.delete();  
                    Toast.makeText(mActivity, "Mail sent.", Toast.LENGTH_SHORT).show();
                }
                else if (requestCode==EMAIL && resultCode==Activity.RESULT_CANCELED)
                {
                    Toast.makeText(mActivity, "Mail canceled.", Toast.LENGTH_SHORT).show();
                }
                else 
                {
                    Toast.makeText(mActivity, "Please try again.", Toast.LENGTH_SHORT).show();
                }
            }   
        }
于 2013-07-09T06:22:43.883 回答