Widget with content provider; impossible to use ReadPermission?

前端 未结 2 947
悲&欢浪女
悲&欢浪女 2021-02-02 13:29

So I\'ve just implemented a widget for my app. It gets its data from the database through my ContentProvider. I define my own read/write-permissions in my manifest,

相关标签:
2条回答
  • 2021-02-02 14:03

    I was able to find the answer here, credit goes to Kostya Vasilyev.

    While the context is actually correct, it is bound to the wrong process.

    So the key is to do this:

    public void onDataSetChanged() {
         // Revert back to our process' identity so we can work with our
         // content provider
         final long identityToken = Binder.clearCallingIdentity();
    
         // Update your cursor or whatever here
         // ...
    
         // Restore the identity - not sure if it's needed since we're going
         // to return right here, but it just *seems* cleaner
         Binder.restoreCallingIdentity(identityToken);
    }
    
    0 讨论(0)
  • 2021-02-02 14:03

    Most likely, it's because you are using the context of the widget, which will be that of the homescreen. Try the following line in your code:

    mCursor = ListWidgetService.this.getContentResolver().query(
                NotePadProvider.CONTENT_VISIBLE_URI, PROJECTION, listWhere, null,
                sortOn);
    

    Note how I replaced the mContext.. A Service is a Context of its own, which should have the correct permission, given your manifest.

    0 讨论(0)
提交回复
热议问题