我目前正在开发一个包含3个Activity的简单Android应用程序,每个Activity包含一些editText和两个按钮。 当我尝试在我的真实设备中运行它时,我注意到它在执行期间非常慢,并且它占用了55 MB的内存RAM空间。 对于简单的应用程序来说,它似乎不是很大的空间?
我的一个活动.java:
package com.larig2.example;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.content.Intent;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Calendar;
import android.support.design.widget.TextInputLayout;
public class content_signin_activity_admin extends AppCompatActivity {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputNomA;
EditText inputPrenomA;
EditText inputCINA;
EditText inputJourA;
Spinner inputMoisA;
EditText inputAnneeA;
Spinner inputAdresseA;
EditText inputTelA;
EditText inputMailA;
EditText inputPwdA;
RadioButton inputFemmeA;
RadioButton inputHommeA;
EditText inputPwdVefA;
String nom;
String prenom;
String cin;
String jour;
String mois;
String annee;
String sexe;
String adresse;
String tel;
String mail;
String pwd;
String pwdvef;
String date;
String tagres="";
private static String url_create_product = "http://192.168.8.104/signup_admin.php";
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_signin_activity_admin);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final Intent contactus = new Intent(this, Contactez_nous.class);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(contactus);
}
});
Spinner spinner = findViewById(R.id.adresse);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Tunisia_State, R.layout.spinnerthem1);
adapter.setDropDownViewResource(R.layout.spinnerthem);
spinner.setAdapter(adapter);
Spinner spinnerMonths = findViewById(R.id.mois);layout
ArrayAdapter<CharSequence> adapterMonths = ArrayAdapter.createFromResource(this,
R.array.Mois_Date, R.layout.spinnerthem1);
adapterMonths.setDropDownViewResource(R.layout.spinnerthem);
spinnerMonths.setAdapter(adapterMonths);
inputNomA = findViewById(R.id.nom);
inputPrenomA = findViewById(R.id.prenom);
inputCINA = findViewById(R.id.CIN);
inputJourA = findViewById(R.id.jour);
inputMoisA = findViewById(R.id.mois);
inputAnneeA = findViewById(R.id.annee);
inputAdresseA = findViewById(R.id.adresse);
inputTelA = findViewById(R.id.tel);
inputMailA = findViewById(R.id.mail);
inputPwdA = findViewById(R.id.pwd);
inputFemmeA = findViewById(R.id.radioButtonFemale);
inputHommeA = findViewById(R.id.radioButtonMale);
inputPwdVefA = findViewById(R.id.pwdvef);
}
public String toMois(String mois){
String nb="00";
switch (mois){
case "Janvier": nb="01";
break;
case "Fevrier": nb="02";
break;
case "Mars": nb="03";
break;
case "Avril": nb="04";
break;
case "Mai": nb="05";
break;
case "Juin": nb="06";
break;
case "Juillet": nb="07";
break;
case "Aoùt": nb="08";
break;
case "September": nb="09";
break;
case "October": nb="10";
break;
case "November": nb="11";
break;
case "December": nb="12";
break;
}
return nb;
}
public boolean checkNom(String ent){
char[] charsN = ent.toCharArray();
if(charsN.length<3){
return false;
}else {
for (char c : charsN) {
if (!Character.isLetter(c)) {
return false;
}
}
}
return true;
}
public boolean checkPrenom(String ent){
char[] charsN = ent.toCharArray();
if(charsN.length<3){
return false;
}else {
for (char c : charsN) {
if (!Character.isLetter(c)) {
return false;
}
}
}
return true;
}
public boolean checkCINTel(String ent){
char[] charsN = ent.toCharArray();
if(!(charsN.length==8)){
return false;
}else {
for (char c : charsN) {
if (!Character.isDigit(c)) {
return false;
}
}
}
return true;
}
public boolean checkAnnee(String ent){
char[] charsN = ent.toCharArray();
if(!(charsN.length==4)){
return false;
}else {
for (char c : charsN) {
if (!Character.isDigit(c)) {
return false;
}
}
}
int var = Integer.parseInt(ent);
if ( !(var <= (Calendar.getInstance().get(Calendar.YEAR) - 18)) && (var > (Calendar.getInstance().get(Calendar.YEAR) - 100))) {
return false;
}
return true;
}
public boolean checkJour(String ent, int a){
char[] charsN = ent.toCharArray();
if(!(charsN.length==2 || charsN.length==1)){
return false;
}else {
for (char c : charsN) {
if (!Character.isDigit(c)) {
return false;
}
}
}
int var= Integer.parseInt(ent);
return ((var <= a) && (var >= 1)) ;
}
public boolean chekDate(String j, String m, String a){
if (checkAnnee(a)) {
if (m == "01" || m == "03" || m == "05" || m == "07" || m == "08" || m == "10" || m == "12") {
if (checkJour(j, 31)) {
return true;
}
} else if (m == "04" || m == "06" || m == "9" || m == "11") {
if (checkJour(j, 30)) {
return true;
}
} else {
if ((Integer.parseInt(a) % 4 == 0)) {
if (checkJour(j, 29)) {
return true;
}
} else {
if (checkJour(j, 28)) {
return true;
}
}
}
}
return false;
}
public boolean checkGenre(String ent){
if (ent=="none") {
return false;
}
return true;
}
public boolean isValidEmailAddress(String email) {
String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
java.util.regex.Matcher m = p.matcher(email);
return m.matches();
}
public boolean isValidPassword(String ent){
char[] charsN = ent.toCharArray();
if(!(charsN.length<6)){
return true;
}
return false;
}
public void signupClickA(View view) {
TextInputLayout til = findViewById(R.id.text_input_layout);
til.setError("You need to enter a name");
cin = inputCINA.getText().toString();
nom = inputNomA.getText().toString();
prenom = inputPrenomA.getText().toString();
jour = inputJourA.getText().toString();
mois = toMois(inputMoisA.getSelectedItem().toString());
annee = inputAnneeA.getText().toString();
adresse = inputAdresseA.getSelectedItem().toString();
tel = inputTelA.getText().toString();
mail = inputMailA.getText().toString();
pwd = inputPwdA.getText().toString();
pwdvef = inputPwdVefA.getText().toString();
date= annee+"-"+mois+"-"+jour;
sexe= "none";
if (inputFemmeA.isChecked()) {
sexe="femme";
}
if (inputHommeA.isChecked()) {
sexe= "homme";
}
if(checkNom(nom)){
if (checkPrenom(prenom)){
if (checkCINTel(cin)) {
if(chekDate(jour,mois,annee)) {
if(checkGenre(sexe)) {
if (checkCINTel(tel)) {
if (isValidEmailAddress(mail)) {
if (isValidPassword(pwd)) {
if(pwdvef.equals(pwd)) {
new CreateNewClient().execute();
}else{
Snackbar.make(view, "La verification de mot de passe échoué", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}else{
Snackbar.make(view, "Le mot de passe doit être au minimum 6 lettres.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}else{
Snackbar.make(view, "Verifier votre Email", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}else{
Snackbar.make(view, "Verifier votre numéro de telephone", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}else{
Snackbar.make(view, "Verifier votre genre (Femme ou Homme)", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}else {
Snackbar.make(view, "Verifier la date de naissance", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}else {
Snackbar.make(view, "Verifier votre Carte d'identité! (8 chiffres)", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}else {
Snackbar.make(view, "Verifier votre Prenom! (Minimum 3 lettres)", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}else {
Snackbar.make(view, "Verifier votre Nom! (Minimum 3 lettres)", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
class CreateNewClient extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(content_signin_activity_admin.this);
pDialog.setMessage("Ex...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("nomC", nom));
params.add(new BasicNameValuePair("prenomC", prenom));
params.add(new BasicNameValuePair("CIN", cin));
params.add(new BasicNameValuePair("date",date ));
params.add(new BasicNameValuePair("sexe", sexe));
params.add(new BasicNameValuePair("adresse", adresse));
params.add(new BasicNameValuePair("tel", tel));
params.add(new BasicNameValuePair("mail", mail));
params.add(new BasicNameValuePair("psw", pwd));
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), ClientSpace.class);
startActivity(i);
finish();
}else if(success == 2){
tagres="2";
}else if(success == 3){
tagres="3";
}
} catch (JSONException e) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if(tagres == "2"){
TextView txt=(TextView) findViewById(R.id.CINError);
Snackbar.make(txt, "Un compte déja créer par cette carte d'identité", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}else if(tagres == "3"){
TextView txt=(TextView) findViewById(R.id.CINError);
Snackbar.make(txt, "Un compte déja créer par cette Email", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.larig2.karhabti.content_signin_activity_admin"
tools:showIn="@layout/activity_content_signin_activity_admin"
android:background="#225D9F"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:id="@+id/scrollView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:contentDescription="@string/app_name"
android:src="@drawable/main_logo"
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/imageView2"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="@string/prompt_firstname"
android:maxLength="15"
android:ems="10"
android:id="@+id/nom"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="@string/prompt_lastname"
android:maxLength="15"
android:ems="10"
android:id="@+id/prenom"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3" />
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3">
<EditText
android:enabled="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="@string/prompt_cin"
android:maxLength="8"
android:ems="10"
android:id="@+id/CIN"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3"
/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:layout_marginTop="29dp">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/jour"
android:maxLength="2"
android:layout_weight="1"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3"
android:hint="@string/prompt_day"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/prompt_month"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal"
android:textColor="#367FC3"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mois"
android:popupBackground="#595959"
/>
</LinearLayout>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/annee"
android:maxLength="4"
android:layout_weight="1"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3"
android:hint="@string/prompt_year"/>
</LinearLayout>
<RadioGroup
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/sexe">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_female"
android:id="@+id/radioButtonFemale"
android:layout_marginStart="25dp"
android:layout_marginTop="31dp"
android:checked="false"
android:textColor="#367FC3"
android:buttonTint="#5EB0E2"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_male"
android:id="@+id/radioButtonMale"
android:layout_marginStart="45dp"
android:layout_marginTop="31dp"
android:checked="false"
android:textColor="#367FC3"
android:buttonTint="#5EB0E2"
/>
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/prompt_Address"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
android:textColor="#367FC3"
android:layout_marginTop="20dp"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/adresse"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:focusable="false"
android:spinnerMode="dropdown"
android:popupBackground="#595959"
android:backgroundTint="#5EB0E2"
/>
<EditText
android:enabled="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:maxLength="8"
android:hint="@string/prompt_tel"
android:ems="10"
android:id="@+id/tel"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3"/>
<EditText
android:enabled="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/prompt_email"
android:maxLength="30"
android:ems="10"
android:id="@+id/mail"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3"/>
<EditText
android:enabled="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/prompt_password"
android:maxLength="15"
android:ems="10"
android:id="@+id/pwd"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3"/>
<EditText
android:enabled="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/prompt_repassword"
android:maxLength="15"
android:ems="10"
android:id="@+id/pwdvef"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3"/>
<EditText
android:enabled="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/prompt_grad"
android:ems="10"
android:id="@+id/editText9"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
android:backgroundTint="#5EB0E2"
android:textColorHint="#367FC3"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="@string/action_sign_in"
android:background="#ffffff"
android:textColor="#2F86C9"
android:onClick="signupClickA"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CINError"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:0)
可以通过Memory Profiler分析应用程序的内存分布来解决。转到视图&gt;工具窗口&gt; Android Profiler。