2015-09-14 164 views
0

答發現: How to send email in background in Android ?從Android發送數據沒有電子郵件或服務器?

我工作的一個Android應用程序及所要求的功能之一是註冊一個新聞信,這是簡單的發送姓名和電子郵件地址的功能。然而,客戶端沒有服務器來託管應用程序來接收這些信息,我想這樣做而不用將它作爲電子郵件發送。我只想說「你已經成功註冊了我們的通訊」或其他內容。

有反正我可以做到這一點嗎?如果是這樣,示例代碼將讚賞,因爲我的背景是在C#中,我正在使用Java。

編輯:即使發送一個隱藏的電子郵件,而不要求客戶端登錄將是可以接受的。

+0

這有點含糊。您正試圖讓通知反彈回應用程序以驗證它們已被註冊?如果沒有服務器,它應該從哪裏反彈回來? – nukeforum

+0

是的,我知道沒有服務器聯繫。這就是爲什麼我問我是否可以在沒有人的情況下做到這一點。即使發送電子郵件而不要求用戶登錄也是可以接受的。 – Eidenai

+0

如果沒有聯繫服務器,該人如何註冊任何內容? – nukeforum

回答

0

我認爲您可能正在尋找的通知方法是Toasts。但是,驗證客戶是否已註冊將不得不在別處發生。

例子:

Toast toast = Toast.makeText(getApplicationContext(), "You've successfully signed up for out newsletter", Toast.LENGTH_LONG); 
toast.show(); 
0

看起來你只是想不服務器演示目的本地顯示的消息,對不對?

首先,我並不認爲你應該在C#中學習Android,原因很多。首先,你需要Java來做Android開發。

這裏簡單的場景。

您有一項活動叫做MainActivity,其中有兩個文本框叫做nameemail

現在,您將有一個按鈕叫submit

一旦用戶輸入姓名和電子郵件地址,並按下提交按鈕,將用戶帶到一個新的活動叫做WelcomeActivity

在android中,您需要一個xml文件來設置您的活動佈局。我會打電話給activity_email.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.maxphone.LoginActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Signup for email" 
    android:id="@+id/welcomeTextView" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginStart="40dp" 
    android:layout_marginTop="20dp" /> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/welcomeTextView" 
    android:layout_marginTop="15dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Name" 
     android:id="@+id/usrnameTextView" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/userEditText" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:maxLines="1"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Email Address" 
     android:id="@+id/emailTextView" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/emailEditText" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:focusableInTouchMode="true" 
     android:maxLines="1"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/email_signed_up_smg" 
     android:id="@+id/loginErrorMsg" 
     android:layout_gravity="center_horizontal|end" 
     android:layout_marginTop="10dp" 
     android:layout_marginEnd="10dp" 
     android:singleLine="false" 
     android:textColor="#ffff0000" 
     android:visibility="invisible" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:id="@+id/signupConfirmBtn" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="40dp" /> 

</LinearLayout> 


</RelativeLayout> 

現在,在您MainActivity

public class MainActivity extends Activity{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    setContentView(R.layout.activity_login); 

    TextView txt = (TextView) findViewById(R.id.welcomeTextView); 
    txt.setTextSize(40); 

    final EditText usrname = (EditText) findViewById(R.id.userEditText); 
    final EditText email = (EditText) findViewById(R.id.emailEditText); 

    final TextView errorMsg = (TextView) findViewById(R.id.emailConfirmMsg); 

    final Button submitBtn = (Button) findViewById(R.id.emailConfirmBtn); 

    // Login Up button behavior 
    submitBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Staring MainActivity 
      Intent i = new Intent(getApplicationContext(), WelcomeActivity.class); 
      startActivity(i); 
      finish(); 
     } 
    } 
} 
} 

這個類將顯示Views(如文本視圖,編輯文本等),並聽取用戶的行爲。一旦submitBtn被點擊,它將創建一個意圖(請做一個研究),並將用戶帶到意圖定義的新活動。

你可以做類似的工作WelcomeActivity顯示歡迎消息,如感謝您註冊!等等。

這是在本地完成的,不需要任何種類的網絡活動。所以這基本上是爲了演示的目的。

祝你好運!

+1

他提到他的問題是他是C#程序員,而不是他正在使用C#編寫Android代碼。 – nukeforum

+0

好的,我可能理解錯了。但這個答案仍然會回答他的問題:) –

相關問題