开发者

Access Context in ContentProvider

开发者 https://www.devze.com 2023-03-24 14:10 出处:网络
I have a ContentProvider Class and a DatabaseHelper Class (extends SQLiteOpenHelper). The ContentProvider instantiates the Helper which needs access to a Context because the constructor requires it:

I have a ContentProvider Class and a DatabaseHelper Class (extends SQLiteOpenHelper). The ContentProvider instantiates the Helper which needs access to a Context because the constructor requires it:

public DBHelper(Context context, AssetFileDescriptor db_as开发者_高级运维set) {
    super(context, DB_NAME, null, 1);

Do you know at least a single way to get the Context from the ContentProvider?

Thanks :)


In your ContentProvider.onCreate method you can pass the result of getContext() to the DBHelper

    @Override
    public boolean onCreate() {
        dbHelper = new DBHelper(getContext(), db_asset);
        return true;
    }


Do you know at least a single way to get the Context from the ContentProvider?

ContentProvider:getContext()


Try this my friend:

SampleClass sample = new SampleClass(this.getContext());

Where this refer to the class that extends the ContentProvider... And .getContext() will get the context of the class that extends the ContentProvider..

Hope this one helps..

0

精彩评论

暂无评论...
验证码 换一张
取 消