2017-03-01 19 views
0

價值我創建了一個在電子郵件發送應用程序列表中顯示的應用程序。獲取的mailto從電子郵件意圖

特殊應用當用戶在一個電子郵件地址點擊時,所有的應用程序email sending apear我的應用程序是其中之一,用戶可以選擇其名單上。

我想保存電子郵件時我的應用程序的用戶點擊。

所有的事情都很好,但我不知道如何從我的應用程序獲得電子郵件?

我知道其他應用程序使用此代碼:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
      "mailto","[email protected]", null)); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body"); 
startActivity(Intent.createChooser(emailIntent, "Send email...")); 

但我不知道如何訪問這些數據放入額外的費用。我只需要從頂端代碼獲取mailto值。

我怎麼能現在得到mailto價值?


編輯:

我有許多共同的電子郵件地址中的一個應用程序,但我不能複製它們。這些電子郵件只是當我點擊他們的Android鏈接所有電子郵件發件人應用程序的鏈接。我想使用我開發的應用程序逐個獲取此電子郵件,並將它們保存在txt文件中。


編輯2: 我找到一種方式來顯示我的所有其他應用電子郵件發件人應用list.i找出不同的應用使用不同的方法把額外的內容發送電子郵件。 爲解決這個問題,並顯示在所有其他應用程序的電子郵件發送列表,我用很多<intent-filter>在我AndroidManifest.xml這樣的應用程序:

許多應用程序使用方法ACTION_VIEW發送電子郵件。如果你想顯示在這種類型的應用程序的應用程序,您必須在您的清單文件使用此代碼:

<intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <data android:scheme="mailto" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <category android:name="android.intent.category.BROWSABLE" /> 
      </intent-filter> 

另一類用於ACTION_SEND爲呼叫電子郵件發件人的應用列表提供意圖應用程序。如果你想顯示在這些類型的應用程序的應用程序,你必須將此代碼添加到您的AndroidManifest.xml文件和你的Activity標籤: 你可以看到3個方法,以提供意圖,我發現這些方式來表達我的應用程序中的所有應用程序的電子郵件發送名單也許另一個方法存在,我不知道現在:

<intent-filter> 
      <action android:name="android.intent.action.SEND"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <data android:scheme="mailto"/> 
     </intent-filter> 

     <intent-filter> 
      <action android:name="android.intent.action.SEND"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <data android:mimeType="text/*"/> 
     </intent-filter> 

     <intent-filter> 
      <action android:name="android.intent.action.SEND"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <data android:mimeType="message/*"/> 
     </intent-filter> 

編輯3:我用這個代碼在我的活動:現在

bundle = intent.getExtras(); 
       if (bundle != null) { 
        for (String key : bundle.keySet()) { 
         Object value = bundle.get(key); 
         Log.d("Extra : ", String.format("%s %s (%s)", key, 
           value.toString(), value.getClass().getName())); 
        } 
       } 

,我也得到LO G:

D/Extra :: android.intent.extra.EMAIL [Ljava.lang.String;@2244b9ca ([Ljava.lang.String;) 

就說明我有額外的一個價值,它的名字是android.intent.extra.EMAIL,但我不能得到這個值。我測試了很多方法!

+0

你怎麼知道有一捆?目前還不清楚你在哪裏得到這個錯誤。如果只有電子郵件,它應該像我下面說的那樣工作:String dataEmail = intent.getDataString(); –

+0

'intent.getDataString()'和'intent.getData()'返回一個空值。我檢查了他們兩個'if' –

+0

那麼捆綁不會返回null?很難知道發送應用程序在做什麼,所以我們必須測試所有的東西。 –

回答

1

你可以這樣做。我只是測試它,它的工作原理。

在送樣的應用程序做到這一點點擊按鈕或TextView的後: 要小心,不要使用它的鏈接上使用自動連接作爲打破它一個TextView。只需用一個簡單的按鈕就可以開始。

<!-- This activity handles "SEND" actions with text data --> 
<intent-filter> 
    <action android:name="android.intent.action.SEND"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
    <data android:scheme="mailto"/> 
</intent-filter> 

和接收應用程序的活動:

// Create the text message with a string 
Intent sendIntent = new Intent(); 
sendIntent.setAction(Intent.ACTION_SENDTO); 
sendIntent.setData(Uri.parse("mailto:[email protected]")); 
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "test subject"); 
sendIntent.putExtra(Intent.EXTRA_TEXT, "sent from first app"); 

// Verify that the intent will resolve to an activity 
if (sendIntent.resolveActivity(getPackageManager()) != null) { 
    startActivity(sendIntent); 
} 
在接收應用程序清單

然後

import static android.content.Intent.EXTRA_SUBJECT; 
import static android.content.Intent.EXTRA_TEXT; 

String dataEmail = intent.getDataString(); 
if (dataEmail != null) { 
    Log.d(TAG, "onCreate: DATA EMAIL: " + dataEmail.substring(7)); 
} 

String subject = intent.getStringExtra(EXTRA_SUBJECT); 
Log.d(TAG, "onCreate: SUBJECT: " + subject); 

String extraText = intent.getStringExtra(EXTRA_TEXT); 
Log.d(TAG, "onCreate: EXTRA TEXT: " + extraText); 

enter image description here

如果你希望它也工作在TextViews那有自動鏈接那麼你需要廣告d這到清單,但是如果你這樣做,那麼只有電子郵件部分將工作。該主題將返回空。

<intent-filter> 
    <action android:name="android.intent.action.VIEW"/> 
    <data android:scheme="mailto"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
    <category android:name="android.intent.category.BROWSABLE"/> 
</intent-filter> 

爲應對這一問題 編輯3如果我在示例應用程序之間傳遞一個包,並使用此:

for (String string :bundle.keySet()) { 
    Log.d(TAG, "onCreate: VALUE: " + bundle.getString(string)); 
} 

然後打印出所有的值了就好了。然而,我知道我輸入的是什麼(字符串),我不知道你使用的是什麼應用程序的開發者。

因此,爲了幫助您能打印:

Log.d(TAG, "onCreate: BUNDLE STRING: " + bundle.toString()); 

和至少當我嘗試它,我得到什麼我投入它的細節,並從那裏我自己看着辦吧(如果我沒不知道)。

+0

reciving活動我有EXTRA_TEXT,EXTRA_EMAIL和EXTRA_SUBJECT錯誤。錯誤顯示此消息對我來說:'如果您可以爲我上傳您的測試項目''不能解析符號EXTRA_TEXT' –

+0

。謝謝 –

+0

和你的代碼是錯誤的清單,因爲你必須在Manifest中使用''「。你如何測試它? –

-1

獲取數據從意圖(在OnCreate中):

Intent intent = getIntent(); 
String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT); 

要添加SENDTO:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO); 
emailIntent.setData(Uri.parse("mailto:[email protected]mple.com")); 

更多:HEREHERE

希望這有助於。

+0

'String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);'返回一個空值。 –

+0

我不需要發送方法,我只需要recive數據。 –