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

리시버에 SQLITE 연동 했을때 오류 질문

0 추천

 

1

2

package com.ondaysteve.administrator.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MyReceiver extends BroadcastReceiver {
    public static final String ROOT_DIR = "/data/data/com.ondaysteve.administrator.myapplication/databases/";
    public SQLiteDatabase db;
    public Cursor cursor;
    ProductDBHelper mHelper;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);

        setDB(this);  //여기
        mHelper = new ProductDBHelper(this);  //여기
        db = mHelper.getWritableDatabase();
        cursor = db.rawQuery("SELECT * FROM tb_onedaysteve", null); 
        startManagingCursor(cursor);  //여기
    }

    public static void setDB(Context ctx) {
        File folder = new File(ROOT_DIR);
        if(folder.exists()) {
        } else {
            folder.mkdirs();
        }
        AssetManager assetManager = ctx.getResources().getAssets();
        File outfile = new File(ROOT_DIR+"onedaysteve5.sqlite");
        InputStream is = null;
        FileOutputStream fo = null;
        long filesize = 0;
        try {
            is = assetManager.open("onedaysteve5.sqlite", AssetManager.ACCESS_BUFFER);
            filesize = is.available();
            if (outfile.length() <= 0) {
                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) {}
    }



    public class ProductDBHelper extends SQLiteOpenHelper {  //새로 생성한 adapter 속성은 SQLiteOpenHelper이다.
        public ProductDBHelper(Context context) {
            super(context, "onedaysteve5.sqlite", null, 1);    // db명과 버전만 정의 한다.
            // TODO Auto-generated constructor stub
        }

        @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
        }

    }
}

메인엑티비티에서는 오류안뜨는데 리시버에서는 오류나네요 제가 이것저것해봤는데 this대신에 다른거 써야될거같은데 뭘써야되는지 모르겠고 startmanagingcursor은 무슨역활을하는건지 여기서 삭제해도되는건지 궁금합니다.

1.this는 뭘 뜻하며 어떻게 오류 해결해야되는지?

2.startmanagingcursor 는 뭘 하는애며 삭제해도 되는지?

 

루비 (1,040 포인트) 님이 2018년 11월 10일 질문

1개의 답변

0 추천

1.this는 뭘 뜻하며 어떻게 오류 해결해야되는지?

 -> this는 현재 클래스를 가리키는 지시자입니다. this 에 들어갈 데이터가 Context이나, Context를 상속 받지 않은  MyReceiver에서 호출 되다보니, 오류가 난것으로 Context 정보를 넘겨주시면 됩니다.

 setDB(context); 

  mHelper = new ProductDBHelper(context);

 

2.startmanagingcursor 는 뭘 하는애며 삭제해도 되는지?

아래 링크를 참조 해 보세요.

http://jepo.tistory.com/126

익명사용자 님이 2018년 11월 12일 답변
...