2013-11-26 80 views
0

我試圖在點擊Button時發送電子郵件,並且我想在發送電子郵件後進入應用的主頁。我試過這段代碼,但它不工作。我可以發送電子郵件,但之後我無法進入主頁,我正在發送電子郵件的同一頁面。如何在一個按鈕內單擊使用兩個意圖?

public void onSendClick(View view) { 
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); 
    intent.setType("Audio/mp3"); 
    intent.putExtra(Intent.EXTRA_SUBJECT, "Attachment from app"); 
    intent.putExtra(Intent.EXTRA_TEXT, "Sending mp3 file "); 

    intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"}); 
    ArrayList<Uri> uris = new ArrayList<Uri>(); 

    uris.add(Uri.fromFile(new File(mFileName.getAbsolutePath()))); 

    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 

    startActivity(Intent.createChooser(intent, "Send mail")); 

    /* Intent i = new Intent(SubmitActivity.this, MainActivity.class); 
    //i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
    this.startActivity(i); */ 
} 

回答

2

嘗試使用startActivityForResult方法並找到在onActivityResult回調主頁。 onActivtyResult回調在電子郵件活動被破壞後被調用。

static final int EMAIL_REQUEST = 1; 

    public void onSendClick(View view) { 
     Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); 
     intent.setType("Audio/mp3"); 
     intent.putExtra(Intent.EXTRA_SUBJECT, "Attachment from app"); 
     intent.putExtra(Intent.EXTRA_TEXT, "Sending mp3 file "); 

     intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"}); 
     ArrayList<Uri> uris = new ArrayList<Uri>(); 

     uris.add(Uri.fromFile(new File(mFileName.getAbsolutePath()))); 

     intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
     startActivityForResult(Intent.createChooser(intent, "Send mail"), EMAIL_REQUEST); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == EMAIL_REQUEST) { 
      //Navigate to home page from here. 
     } 
    } 
+0

非常感謝,其實我沒有做好,現在它的工作原理,非常感謝... :-) –

0

如果您是從網頁調用活動的活動,嘗試

startActivity(..); 
finish(); 
+0

不,不行。 –

相關問題