How do I catch content provider initialize?

孤街醉人 提交于 2019-11-29 02:36:51

问题


As we know content provider loads on application run. But I want to make some operations before content provider will launch. How do I catch this operation? Before content provider's onCreate method would be called


回答1:


I think Ive found solution. Ive created my custom application class and overridden attachBaseContext method

<application android:name=".ApplicationController" ...>

public class ApplicationController extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        // some of your own operations before content provider will launch
    }
}



回答2:


But I want to make some operations before content provider will launch...Before content provider's onCreate method would be called

AFAIK, that is not possible. onCreate() of your ContentProvider will be the first opportunity you have to run code, moments after your process is forked. That occurs even before onCreate() is called on Application, if I understand correctly.




回答3:


My solution requires use of the call(Uri, String, String, Bundle) API (so, it's not fully backward-compatible). But I have the stuff that I want the ContentProvider to prepare before it is used in my override of that call method. Then I do getContentResolver().call(BASE_URI, METHOD, null, Bundle.EMPTY) in my Application.onCreate(). Essentially, it defers that stuff until after my Application is being created, which is what we expected the ContentProvider's onCreate to be doing naturally.



来源:https://stackoverflow.com/questions/9873669/how-do-i-catch-content-provider-initialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!