Created
January 19, 2015 10:00
-
-
Save wasnot/12f109b3d655cbc87ec3 to your computer and use it in GitHub Desktop.
ContentProviderでSharedPreferencesを使う ref: http://qiita.com/wasnot/items/8329409a46f31f1724b3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestProvider extends ContentProvider { | |
... | |
private SharedPreferences mPrefs; | |
@Override | |
public boolean onCreate() { | |
LogUtil.d(TAG, "onCreate " + Binder.getCallingPid() + ", " | |
+ Thread.currentThread().getName()); | |
mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); | |
return true; | |
} | |
@Override | |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, | |
String sortOrder) { | |
// URIの整合性を確認 | |
checkUri(uri); | |
if (uri == null || uri.getPathSegments() == null) { | |
} else if (TABLE.equals(uri.getPathSegments().get(0))) { | |
// すごく例えが悪いですが、 | |
// 例えばSharedPreferencesの中身をそのままkey,valueでいれてしまう。 | |
MatrixCursor cursor = new MatrixCursor(new String[] { | |
"key", "value" | |
}); | |
Map all = mPrefs.getAll(); | |
Object value; | |
for (String key : selectionArgs) { | |
if (all.containsKey(key)) { | |
value = all.get(key); | |
cursor.addRow(new Object[] { | |
key, value | |
}); | |
} | |
} | |
return cursor; | |
} else { | |
//テーブルによって出し分けるなど。 | |
} | |
throw new UnsupportedOperationException(""); | |
} | |
// もしもその他作業には対応していなければExceptionをthrowするなど。 | |
@Override | |
public Uri insert(Uri uri, ContentValues values) { | |
checkUri(uri); | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { | |
checkUri(uri); | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public int delete(Uri uri, String selection, String[] selectionArgs) { | |
checkUri(uri); | |
throw new UnsupportedOperationException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment