我的应用程序消耗了大量的缓存后台进程

时间:2016-10-21 22:24:20

标签: android

我有一个Android应用程序,在drawable文件夹中有100个图像。每个活动使用大约10个图像,在我打开10个不同的活动后,我的缓存后台进程消耗200mb。即使是priror活动关闭,缓存的后台进程也是巨大的。 我已经阅读过有关缓存后台进程的内容,我知道它不是那么严重的问题,但在我的情况下它需要大约200mb这对我来说是巨大的。 我应该担心吗?我该如何处理?

编辑:添加代码 我正在使用一个具有静态实例的类,并且它正在另一个活动中使用

public class BazaNaPrasanja{

// to create a database
static public VozackaDbHelper vozackaDbHelper;
// to use the database
static public SQLiteDatabase database;

// cursor used for the table in the database
static public Cursor cursorZnaci4;

public BazaNaPrasanja(Context context)
{
    vozackaDbHelper = new VozackaDbHelper(context);
    database = vozackaDbHelper.getWritableDatabase();
}

public void queryCursorZnaci4()
{
    cursorZnaci4 = database.query(VozackaDbContract.TABLE_NAME_ZNACI4,null,null,null,null,null,null);
    cursorZnaci4.moveToFirst();
}
}

和使用使用静态实例创建的类的活动

公共类Znaci4扩展了活动{

@Override
protected void onCreate(Bundle b)
{
    super.onCreate(b);
    setContentView(R.layout.znaci_4);

    int columnIndexPrasanje = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_PRASANJE);
    int columnIndexSlika1 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_SLIKA1);
    int columnIndexSlika2 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_SLIKA2);
    int columnIndexSlika3 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_SLIKA3);
    int columnIndexSlika4 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_SLIKA4);
    int columnIndexOdgovor1 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_ODGOVOR1);
    int columnIndexOdgovor2 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_ODGOVOR2);
    int columnIndexOdgovor3 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_ODGOVOR3);
    int columnIndexOdgovor4 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_ODGOVOR4);
    int columnIndexOdgovor5 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_ODGOVOR5);
    int columnIndexOdgovor6 = BazaNaPrasanja.cursorZnaci4.getColumnIndex(VozackaDbContract.COLUMN_NAME_ODGOVOR6);

// a textview that is getting the text from the database and sets it's text to it
    TextView prasanjeTV = (TextView) findViewById(R.id.prasanjeTV);
    String prasanjeStr = BazaNaPrasanja.cursorZnaci4.getString(columnIndexPrasanje);
    prasanjeTV.setText(prasanjeStr);

// imageview that gets the name of the drawable from the database and sets the background to that resource
    ImageView slika1IV = (ImageView) findViewById(R.id.slika1);
    String slika1Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexSlika1);
    int slika1Ref = this.getResources().getIdentifier(slika1Str,"drawable", getPackageName());
    slika1IV.setBackgroundResource(slika1Ref);

    ImageView slika2IV = (ImageView) findViewById(R.id.slika2);
    String slika2Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexSlika2);
    int slika2Ref = this.getResources().getIdentifier(slika2Str,"drawable", getPackageName());
    slika2IV.setBackgroundResource(slika2Ref);

    ImageView slika3IV = (ImageView) findViewById(R.id.slika3);
    String slika3Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexSlika3);
    int slika3Ref = this.getResources().getIdentifier(slika3Str,"drawable", getPackageName());
    slika3IV.setBackgroundResource(slika3Ref);

    ImageView slika4IV = (ImageView) findViewById(R.id.slika4);
    String slika4Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexSlika4);
    int slika4Ref = this.getResources().getIdentifier(slika4Str,"drawable", getPackageName());
    slika4IV.setBackgroundResource(slika4Ref);


// textview just getting value from database and sets it's text to it
    TextView odgovor1TV = (TextView) findViewById(R.id.odgovor1TV);
    String odgovor1Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexOdgovor1);
    odgovor1TV.setText(odgovor1Str);

    TextView odgovor2TV = (TextView) findViewById(R.id.odgovor2TV);
    String odgovor2Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexOdgovor2);
    odgovor2TV.setText(odgovor2Str);

    TextView odgovor3TV = (TextView) findViewById(R.id.odgovor3TV);
    String odgovor3Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexOdgovor3);
    odgovor3TV.setText(odgovor3Str);

    TextView odgovor4TV = (TextView) findViewById(R.id.odgovor4TV);
    String odgovor4Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexOdgovor4);
    odgovor4TV.setText(odgovor4Str);

    TextView odgovor5TV = (TextView) findViewById(R.id.odgovor5TV);
    String odgovor5Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexOdgovor5);
    odgovor5TV.setText(odgovor5Str);

    TextView odgovor6TV = (TextView) findViewById(R.id.odgovor6TV);
    String odgovor6Str = BazaNaPrasanja.cursorZnaci4.getString(columnIndexOdgovor6);
    odgovor6TV.setText(odgovor6Str);

}


// actually it start's the same activity just with different position of the cursor(with different data)
public void slednoPrasanje(View view) {
    BazaNaPrasanja.cursorZnaci4.moveToNext();
    int pos = BazaNaPrasanja.cursorZnaci4.getPosition();
    if(pos == 25) BazaNaPrasanja.cursorZnaci4.moveToFirst();
    startActivity(new Intent(this, Znaci4.class));
    finish();   // I destroy the old activity but the cached background proccess takes 200mb after I open 10 or less than it activities
}



}

}

一些无用的代码只是为了节省一些空间而不是压倒你。

0 个答案:

没有答案