Android应用中的内存效率

时间:2015-10-14 04:25:20

标签: android memory

我是Android新手,我的英语不好。请理解我。我创建了一个显示待办事项列表的Android应用程序。

下图显示Android模拟器内存​​分配48MB(在我的手机中,140MB)。

Android emulator memory

如何才能提高效率?或者我应该保持不变?

我的源代码如下:

public class AThingToDo_Main extends Activity {
    ListView toToListView;
    ImageButton plusButton, minusButton;

    CustomDialog customDialog;
    AddThingDialog addThingDialog;
    DatabaseHandler databaseHandler;
    public static ArrayList<AThingToDo> toDoArrayList;
    ListViewAdapter adapter;    
    int mYear, mMonthOfYear, mDayOfMonth, 
    int id=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_athing_to_do__main);

        Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonthOfYear = c.get(Calendar.MONTH)+1;
        mDayOfMonth = c.get(Calendar.DAY_OF_MONTH);

        Log.d("TODAYDUE", String.valueOf(mYear) + " " + String.valueOf(mMonthOfYear) + " " + String.valueOf(mDayOfMonth));
        Log.d("WHODUE",String.valueOf( mMonthOfYear));

        databaseHandler = new DatabaseHandler(this);
        toDoArrayList = (ArrayList) databaseHandler.getAllThings();

        for (int i = 0; i < toDoArrayList.size(); i++) {
            int due = toDoArrayList.get(i).getDueDate();
            int year = due/10000;
            int month = (due - year*10000)/100;
            int day = due %100;

            Log.d("TODAYDUE",String.valueOf(year) +" "+ String.valueOf(month) +" "+String.valueOf(day) );

            if(toDoArrayList.get(i).getIsDone()==1 && isDueDone(year,month,day)  ){
                toDoArrayList.remove(i);
                databaseHandler.deleteWord(toDoArrayList.get(i));
            }
        }
            for (int i = 0; i < toDoArrayList.size(); i++) {
                 Log.d("ID", String.valueOf(toDoArrayList.get(i).get_id()));
             }

        ListViewSetting();

        plusButton = (ImageButton) findViewById(R.id.plusButton);
        plusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                addThingDialog = new AddThingDialog(AThingToDo_Main.this, databaseHandler);
                addThingDialog.show();

            }
        });
    }

    ////////////////////////////////////////////////////////////LIST SETTING
    public void ListViewSetting() {
        toToListView = (ListView) findViewById(R.id.toDoListView);

        adapter = new ListViewAdapter(this);
        toToListView.setAdapter(adapter);
        toToListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int _position = position;
                customDialog = new CustomDialog(AThingToDo_Main.this, position, databaseHandler);
                customDialog.show();
            }
        });
    }

    private boolean isDueDone(int year, int month , int day){

        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.set(mYear, mMonthOfYear-1, mDayOfMonth);
        endDate.set(year, month-1,day);
        long differday = 0;
        long b = (endDate.getTimeInMillis() - startDate.getTimeInMillis()) /1000;
        differday = b/(60*60*24);

        Log.i("DUEINFO", endDate.getTimeInMillis() + " " + startDate.getTimeInMillis() + "differDAY  : " + differday);
        if( differday <0){
            return true;
        }else{
            return false;
        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_action, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String text = null;

        switch(item.getItemId()){
            case R.id.action_history:
                //Intent intent = new Intent(AThingToDo_Main.this,History.class );
               // startActivity(intent);
                break;
        }
        return true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("dd","dd");
        databaseHandler.close();
        Intent intent = new Intent("com.example.user.athingtodo.WIDGETQUPDATENEED");
        sendBroadcast(intent);

        this.onDestroy();
    }

}

如果触摸添加按钮,请调用AddThingDialog

public class AddThingDialog extends Dialog {
    Context mContext;

    int mYear, mMonthOfYear, mDayOfMonth;
    int dueYear, dueMonth, dueDay;
    int mHour, mMinute;

    boolean isCheckeddddd=false;

    TextView dueDate;
    EditText addToDoThing;

    CheckBox notificationCB ;
    DatePickerDialog datePickerDialog;
    TimePickerDialog timePickerDialog;
    DatabaseHandler databaseHandler ;


    AlarmManagerBroadCastReceiver alarmManagerBroadCastReceiver ;
    int idPosition=1 ;

    public AddThingDialog(Context context, final DatabaseHandler databaseHandler) {
        super(context);
        alarmManagerBroadCastReceiver = new AlarmManagerBroadCastReceiver();
        mContext = context;
        if(AThingToDo_Main.toDoArrayList.isEmpty()){idPosition=1;}
        else {
            this.idPosition = AThingToDo_Main.toDoArrayList.get(AThingToDo_Main.toDoArrayList.size() - 1).get_id()+1;
        }
        Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonthOfYear = c.get(Calendar.MONTH)+1;
        mDayOfMonth = c.get(Calendar.DAY_OF_MONTH);
        dueYear = mYear;
        dueMonth = mMonthOfYear;
        dueDay = mDayOfMonth;
        this.databaseHandler = databaseHandler;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.addthing_dialog);
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);


        addToDoThing = (EditText)findViewById(R.id.AddThingDialogEditText);

        timePick(false);
        datePick();

        ImageButton continueButton = (ImageButton)findViewById(R.id.continueImageButtonDialog);
        ImageButton addButton = (ImageButton)findViewById(R.id.addImageButtonDialog);

        continueButton.setBackgroundColor(Color.TRANSPARENT);
        addButton.setBackgroundColor(Color.TRANSPARENT);

        continueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int due = dueYear*10000+dueMonth*100+dueDay;
                String text = addToDoThing.getText().toString();
                AThingToDo thing;

                if(notificationCB.isChecked()) {
                    int time = mHour*100+mMinute;

                    thing = new AThingToDo(idPosition, text, due, 0, time);                      alarmManagerBroadCastReceiver.SetAlarm(mContext,mHour,mMinute,idPosition,text,due);
                }else{
                    thing = new AThingToDo(idPosition, text, due, 0, -1);
                }
                databaseHandler.addThing(thing);
                AThingToDo_Main.toDoArrayList.add(thing);
                Log.d("ADDID", String.valueOf(idPosition));
                idPosition++;
                addToDoThing.setText("");

            }
        });
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int due = dueYear*10000+dueMonth*100+dueDay;
                String text = addToDoThing.getText().toString();
                AThingToDo thing;

                if(notificationCB.isChecked()) {
                    int time = mHour*100+mMinute;
                    thing = new AThingToDo(idPosition, text, due, 0, time);
                    alarmManagerBroadCastReceiver.SetAlarm(mContext,mHour,mMinute,idPosition,text,due);
                }else{
                    thing = new AThingToDo(idPosition, text, due, 0, -1);
                }
                databaseHandler.addThing(thing);
                AThingToDo_Main.toDoArrayList.add(thing);
                idPosition++;
                ((AThingToDo_Main)mContext).ListViewSetting();

                dismiss();
            }
        });
    }

    public AddThingDialog(Context context, final DatabaseHandler databaseHandler,String text, int year,int month, int day,boolean isCheck, int hour,int minute){
        super(context);

        alarmManagerBroadCastReceiver = new AlarmManagerBroadCastReceiver();
        mContext = context;
        if(AThingToDo_Main.toDoArrayList.isEmpty()){idPosition=1;}
        else {
            this.idPosition = AThingToDo_Main.toDoArrayList.get(AThingToDo_Main.toDoArrayList.size() - 1).get_id()+1;
        }
        Calendar c = Calendar.getInstance();
        mYear = year;
        mMonthOfYear = month;
        mDayOfMonth = day;
        dueYear = year;
        dueMonth = month;
        dueDay = day;
        this.databaseHandler = databaseHandler;

        mHour =hour;
        mMinute =minute;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.addthing_dialog);
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);

        addToDoThing = (EditText)findViewById(R.id.AddThingDialogEditText);
        if( text != null)addToDoThing.setText(text);

        Log.d("WHOCHECK",String.valueOf(isCheck));


        timePick(isCheck);
        datePick();

        ImageButton continueButton = (ImageButton)findViewById(R.id.continueImageButtonDialog);
        ImageButton addButton = (ImageButton)findViewById(R.id.addImageButtonDialog);

        continueButton.setBackgroundColor(Color.TRANSPARENT);
        addButton.setBackgroundColor(Color.TRANSPARENT);

        continueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int due = dueYear*10000+dueMonth*100+dueDay;
                String text = addToDoThing.getText().toString();
                AThingToDo thing;

                if(notificationCB.isChecked()) {
                    int time = mHour*100+mMinute;

                    thing = new AThingToDo(idPosition, text, due, 0, time);
                    alarmManagerBroadCastReceiver.SetAlarm(mContext,mHour,mMinute,idPosition,text,due);
                }else{
                    thing = new AThingToDo(idPosition, text, due, 0, -1);
                }
                databaseHandler.addThing(thing);
                AThingToDo_Main.toDoArrayList.add(thing);
                Log.d("ADDID", String.valueOf(idPosition));
                idPosition++;
                addToDoThing.setText("");
            }
        });
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int due = dueYear*10000+dueMonth*100+dueDay;
                String text = addToDoThing.getText().toString();
                AThingToDo thing;

                if(notificationCB.isChecked()) {
                    int time = mHour*100+mMinute;
                    thing = new AThingToDo(idPosition, text, due, 0, time);

                    alarmManagerBroadCastReceiver.SetAlarm(mContext,mHour,mMinute,idPosition,text,due);
                }else{
                    thing = new AThingToDo(idPosition, text, due, 0, -1);
                }

                databaseHandler.addThing(thing);
                AThingToDo_Main.toDoArrayList.add(thing);

                idPosition++;
                ((AThingToDo_Main)mContext).ListViewSetting();

                dismiss();
            }
        });

    }


    @Override
    public void show()
    {
        super.show();
    }

    @Override
    public void dismiss()
    {
        super.dismiss();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    void timePick(boolean check){
        final TextView notificationTextView = (TextView)findViewById(R.id.add_notification_textView);
        notificationCB = (CheckBox)findViewById(R.id.add_notification_checkBox);
        if(check){
            notificationCB.setChecked(true);
            notificationTextView.setText(String.valueOf(mHour) + " : " + String.valueOf(mMinute));
            isCheckeddddd = true;
        }
        timePickerDialog = new TimePickerDialog(mContext, timeListener, mHour, mMinute, false);
        timePickerDialog.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                databaseHandler.close();
            }
        });
        timePickerDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                new AddThingDialog(mContext, databaseHandler, addToDoThing.getText().toString(), dueYear, dueMonth, dueDay, isCheckeddddd, mHour, mMinute).show();
            }
        });
        notificationCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    isCheckeddddd = true;
                    //  new DatePickerDialog(MainActivity.this, dateSetListener, year, month, day).show();
                    timePickerDialog.show();
                    dismiss();
                } else {
                    isCheckeddddd = false;
                    notificationTextView.setText("Notification");
                }
            }
        });
        notificationTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (notificationCB.isChecked()) {

                    timePickerDialog.show();
                    dismiss();
                }
            }
        });
    }
    void datePick(){


        dueDate = (TextView)findViewById(R.id.addDueDateDialog);
        dueDate.setText(String.valueOf(dueYear) + " / " + String.valueOf(dueMonth) + " / " + String.valueOf(dueDay));
        dueDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePickerDialog = new DatePickerDialog(mContext, listener, mYear, mMonthOfYear-1, mDayOfMonth);
                datePickerDialog.setOnDismissListener(new OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        databaseHandler.close();
                    }
                });
                datePickerDialog.setOnCancelListener(new OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        new AddThingDialog(mContext,databaseHandler,addToDoThing.getText().toString(),dueYear,dueMonth,dueDay,isCheckeddddd,mHour,mMinute).show();
                    }
                });
                datePickerDialog.show();

                dismiss();
            }
        });
//        datePickerDialog.show();
        // datePicker.init(mYear, mMonthOfYear, mDayOfMonth, listener);

    }
    TimePickerDialog.OnTimeSetListener timeListener =new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour =hourOfDay;
            mMinute= minute;
            new AddThingDialog(mContext, databaseHandler, addToDoThing.getText().toString(), dueYear, dueMonth, dueDay, isCheckeddddd, mHour, mMinute).show();

            timePickerDialog.dismiss();

        }
    };
    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            dueYear=year;
            dueMonth=monthOfYear+1;
            dueDay=dayOfMonth;
            new AddThingDialog(mContext, databaseHandler, addToDoThing.getText().toString(), dueYear, dueMonth, dueDay, isCheckeddddd, mHour, mMinute).show();

            datePickerDialog.dismiss();
        }
    };
}

我无法在对话框上显示对话框,因此我创建了两个构造函数。一个是正​​常的, 另一个是在datepicker对话框或timepicker对话框中解雇自己。

0 个答案:

没有答案