마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

DB를 asset에넣고 새로 생성해서 참조하는 어플 렉현상 문제

0 추천
public class SlotMachine extends Activity {

 public static final String ROOT_DIR = Environment
   .getExternalStorageDirectory().getAbsolutePath()
   + "/com.seail.random_situation/databases";
 public SQLiteDatabase db; 
 public static Cursor cursor;
 ProductDBHelper mHelper; 
 static int menu_number = 1;
 static int icon_num;

 Intent intent;
 boolean auto = false;
 private TextView[] arTv = new TextView[7];
 int time;

 String sum_text;
 Animation flowAnim;
 ThreadAnim thre_ani;
 String[][] arMenu = { //db 테이블
   { "school_when", "school_who", "school_where", "school_how", "school_what", "bonus", "coin" },
   { "company_when", "company_who", "company_where", "company_how", "company_what", "bonus", "coin" },
   { "traveling_when", "traveling_who", "traveling_where", "traveling_how", "traveling_what", "bonus", "coin" },
   { "drinkingparty_when", "drinkingparty_who", "drinkingparty_where", "drinkingparty_how", "drinkingparty_what", "bonus", "coin" },
   { "comic_when", "comic_who", "comic_where", "comic_how", "comic_what", "bonus", "coin" } };
 int ar_num = 0;
 int count_c;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.slotmachine);

  setDB(); // setDB에 Context가 없다.
  mHelper = new ProductDBHelper(this);

  intent = getIntent();
  icon_num = intent.getIntExtra("Icon_num", 0);

  db = mHelper.getWritableDatabase();
  Log.e("test", "getWritableDatabase open");

  arTv[0] = (TextView) findViewById(R.id.text_when);
  arTv[1] = (TextView) findViewById(R.id.text_who);
  arTv[2] = (TextView) findViewById(R.id.text_where);
  arTv[3] = (TextView) findViewById(R.id.text_how);
  arTv[4] = (TextView) findViewById(R.id.text_what);
  arTv[5] = (TextView) findViewById(R.id.text_bonus);
  arTv[6] = (TextView) findViewById(R.id.text_coin);

  flowAnim = AnimationUtils.loadAnimation(this, R.anim.flow);
  thre_ani = new ThreadAnim();
  sum_text = "";
 }

 class ThreadAnim extends Thread {

  public void run() {
   cursor.moveToFirst();
   flowAnim.setAnimationListener(new FlowAnimationListener());
   time = 1000;
   for (int i = 0; i < cursor.getCount() - 1; i++) {
    sum_text = cursor.getString(1);
    mHandler.sendEmptyMessage(0);
    cursor.moveToNext();
    try {
     Thread.sleep(time);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    time += 100;
   }
   Log.e("test", "ThreadAnim open");
   
  }
 }

 final Handler mHandler = new Handler() {
  public void handleMessage(Message msg) {
   Log.e("test", "Handler time = " + time);
   arTv[menu_number - 1].setText(sum_text);
   flowAnim.setDuration(time);
   arTv[menu_number - 1].startAnimation(flowAnim);
  }
 };

 private final class FlowAnimationListener implements

 Animation.AnimationListener {
  public void onAnimationEnd(Animation animation) {
   // TODO Auto-generated method stub
   Toast.makeText(getApplicationContext(), "애니메이션 종료됨", 500).show();
  }

  public void onAnimationRepeat(Animation animation) {
   // TODO Auto-generated method stub
  }

  public void onAnimationStart(Animation animation) {
   // TODO Auto-generated method stub
  }
 }

 public void setDB() {

  Log.e("test", "setDB open");
  File folder = new File(ROOT_DIR);
  if (folder.exists()) {
  } else {
   folder.mkdirs();
   Log.e("test", "make folder");
  }
  Log.e("test", "AssetManager before");
  AssetManager assetManager = getResources().getAssets(); // ctx가 없다.
  Log.e("test", "AssetManager after");
  File outfile = new File(ROOT_DIR + "/roulette.db");
  InputStream is = null;
  FileOutputStream fo = null;
  long filesize = 0;
  try {
   is = assetManager.open("roulette.db", AssetManager.ACCESS_BUFFER);
   filesize = is.available();
   Log.e("test", "try assetManager open");
   if (outfile.length() <= 0) {
    Log.e("test", "copyDB");
    byte[] tempdata = new byte[(int) filesize];
    is.read(tempdata);
    is.close();
    outfile.createNewFile();
    fo = new FileOutputStream(outfile);
    fo.write(tempdata);
    fo.close();
   } else {
   }
  } catch (IOException e) {
  }
 }

 class ProductDBHelper extends SQLiteOpenHelper {
  
  public ProductDBHelper(Context context) {
   super(context, ROOT_DIR + "/roulette.db", null, 1); 
   // TODO Auto-generated constructor stub
   Log.e("test", "openDB");
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
  }
 }
}

(미완성 코드입니다. 글씨제한때문에 중간중간 렉과 관련없었던 구간은 지웠습니다.)

 

안녕하세요 안드로이드 초보 개발자 R0R0 입니다.

assets에 db를 넣고 복사해서 여러개의 테이블을 참조하여 애니메이션효과를 주는 코드입니다.

db의 테이블은 27개 이며 개당 30줄이 되지않는 소규모 테이블 입니다. 

위 형식대로 코드를 짯는데 DB를 assets에 넣고나니 메인스레드에서  너무많은 작업을 한다는 메세지와 함께 엄청나게 느려지네요

Log 내용 : 39 frames! The application may be doing too much work on its main thread.

초보자인 저로써는 어디를 손봐야할지 도저히 감이 잡히지않아 이렇게 자문을 구해봅니다.

어떤문제인지 어느 부분을 수정하면되는지 만이라도 좋으니 

친절한 답변 부탁 드립니다.

 

R0R0 (2,610 포인트) 님이 2014년 12월 4일 질문
R0R0님이 2014년 12월 4일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...