美文网首页
ContentProvider 使用sharePreferenc

ContentProvider 使用sharePreferenc

作者: 梦凝天 | 来源:发表于2016-11-29 16:11 被阅读93次
    public class UserProvider extends ContentProvider {
        private static final int USE_INFO = 1;
    
        private SpCache sp;
        private UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    
        @Override
        public boolean onCreate() {
    
            sp = SpCache.createCache(getContext(), PerferenceContants.PREF_CONFIG);
            mUriMatcher.addURI("com.example.UserProvider", "use_info", USE_INFO);
            return true;
        }
    
        @Nullable
        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            Log.d("TB", "userProvider-->" + uri);
            int code = mUriMatcher.match(uri);
            Log.d("TB", "code==>" + code);
            MatrixCursor vMatrixCursor = null;
            switch (code) {
                case UriMatcher.NO_MATCH:
                    return null;
                case USE_INFO:
                    String uuid = sp.getString("uuid", "");
                    String sn = sp.getString("sn", "");
                    String phone = sp.getString("phone", "");
                    vMatrixCursor = new MatrixCursor(new String[]{"uuid", "sn", "phone"}, 1);
                    vMatrixCursor.addRow(new Object[]{uuid, sn, phone});
                    return vMatrixCursor;
    
            }
            return null;
        }
    
        @Nullable
        @Override
        public String getType(Uri uri) {
            return null;
        }
    
        @Nullable
        @Override
        public Uri insert(Uri uri, ContentValues values) {
            return null;
        }
    
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            return 0;
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            return 0;
        }
    }
    
    <provider   
     android:name=".UserProvider"   
     android:authorities="com.example.UserProvider"    
    android:exported="true"   
     android:grantUriPermissions="true">
    </provider>
    

    相关文章

      网友评论

          本文标题:ContentProvider 使用sharePreferenc

          本文链接:https://www.haomeiwen.com/subject/plzapttx.html