下面列出了android.content.ContentResolver#notifyChange ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void addSongsToPlaylist(List<Song> songs, v.blade.library.Playlist list, OperationCallback callback)
{
if(list.getSources().getSourceByPriority(0).getSource() != SOURCE_LOCAL_LIB) {callback.onFailure(); return;}
int count = list.getContent().size();
ContentValues[] values = new ContentValues[songs.size()];
for (int i = 0; i < songs.size(); i++)
{
SongSources.SongSource local = songs.get(i).getSources().getLocal();
if(local == null) {callback.onFailure(); return;}
values[i] = new ContentValues();
values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i + count + 1);
values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, (long) songs.get(i).getSources().getLocal().getId());
}
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", (long) list.getSources().getLocal().getId());
ContentResolver resolver = LibraryService.appContext.getContentResolver();
int num = resolver.bulkInsert(uri, values);
resolver.notifyChange(Uri.parse("content://media"), null);
list.getContent().addAll(songs);
callback.onSucess(null);
}
public static void appendToPlaylist(Context context, Playlist playlist, Song song) {
Uri uri = MediaStore.Audio.Playlists.Members
.getContentUri("external", playlist.getPlaylistId());
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER,
getPlaylistSize(context, playlist.getPlaylistId()));
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, song.getSongId());
ignoreSingleContentUpdate();
resolver.insert(uri, values);
ignoreSingleContentUpdate();
resolver.notifyChange(Uri.parse("content://media"), null);
}
public static void appendToPlaylist(Context context, Playlist playlist, List<Song> songs) {
Uri uri = MediaStore.Audio.Playlists.Members
.getContentUri("external", playlist.getPlaylistId());
ContentResolver resolver = context.getContentResolver();
int startingCount = getPlaylistSize(context, playlist.getPlaylistId());
ContentValues[] values = new ContentValues[songs.size()];
for (int i = 0; i < songs.size(); i++) {
values[i] = new ContentValues();
values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, startingCount + i);
values[i].put(
MediaStore.Audio.Playlists.Members.AUDIO_ID,
songs.get(i).getSongId());
}
ignoreSingleContentUpdate();
resolver.bulkInsert(uri, values);
ignoreSingleContentUpdate();
resolver.notifyChange(Uri.parse("content://media"), null);
}
/**
* Set screen brightness
* <p>Need to add permissions {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
*
* @param brightness Brightness value
*/
public static boolean setBrightness(Context context, @IntRange(from = 0, to = 255) final int brightness) {
ContentResolver resolver = context.getContentResolver();
boolean b = Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
resolver.notifyChange(Settings.System.getUriFor("screen_brightness"), null);
return b;
}
public static void insertAlbumArt(@NonNull Context context, int albumId, String path) {
ContentResolver contentResolver = context.getContentResolver();
Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
contentResolver.delete(ContentUris.withAppendedId(artworkUri, albumId), null, null);
ContentValues values = new ContentValues();
values.put("album_id", albumId);
values.put("_data", path);
contentResolver.insert(artworkUri, values);
contentResolver.notifyChange(artworkUri, null);
}
protected void onEndTransaction(boolean callerIsSyncAdapter)
{
Set<Uri> changed;
synchronized (mChangedUris)
{
changed = new HashSet<Uri>(mChangedUris);
mChangedUris.clear();
}
ContentResolver resolver = getContext().getContentResolver();
for (Uri uri : changed)
{
boolean syncToNetwork = !callerIsSyncAdapter && syncToNetwork(uri);
resolver.notifyChange(uri, null, syncToNetwork);
}
}
/**
* 保存亮度设置状态
*
* @param resolver
* @param brightness
*/
public static void saveBrightness(ContentResolver resolver, int brightness) {
Uri uri = Settings.System.getUriFor("screen_brightness");
Settings.System.putInt(resolver, "screen_brightness", brightness);
// resolver.registerContentObserver(uri, true, myContentObserver);
resolver.notifyChange(uri, null);
}
/**
* 设置亮度(每30递增)
*
* @param activity
*/
public static void setBrightness(Activity activity) {
ContentResolver resolver = activity.getContentResolver();
Uri uri = Settings.System.getUriFor("screen_brightness");
int nowScreenBri = getScreenBrightness(activity);
nowScreenBri = nowScreenBri <= 225 ? nowScreenBri + 30 : 30;
System.out.println("nowScreenBri==" + nowScreenBri);
Settings.System.putInt(resolver, "screen_brightness", nowScreenBri);
resolver.notifyChange(uri, null);
}
private void notifyUris(Uri affectedUri) {
final ContentResolver contentResolver =
getContext().getContentResolver();
if (contentResolver != null) {
contentResolver.notifyChange(affectedUri, null);
contentResolver
.notifyChange(DevicesContract
.DeviceManufacturer.CONTENT_URI, null);
}
}
/**
* 保存亮度设置状态
*/
public void saveBrightness(ContentResolver resolver, int brightness) {
Uri uri = Settings.System
.getUriFor("screen_brightness");
Settings.System.putInt(resolver, "screen_brightness",
brightness);
// resolver.registerContentObserver(uri, true, myContentObserver);
resolver.notifyChange(uri, null);
}
public static void insertAlbumArt(@NonNull Context context, int albumId, String path) {
ContentResolver contentResolver = context.getContentResolver();
Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
contentResolver.delete(ContentUris.withAppendedId(artworkUri, albumId), null, null);
ContentValues values = new ContentValues();
values.put("album_id", albumId);
values.put("_data", path);
contentResolver.insert(artworkUri, values);
contentResolver.notifyChange(artworkUri, null);
}
protected void onEndTransaction(boolean callerIsSyncAdapter)
{
Set<Uri> changed;
synchronized (mChangedUris)
{
changed = new HashSet<Uri>(mChangedUris);
mChangedUris.clear();
}
ContentResolver resolver = getContext().getContentResolver();
for (Uri uri : changed)
{
boolean syncToNetwork = !callerIsSyncAdapter && syncToNetwork(uri);
resolver.notifyChange(uri, null, syncToNetwork);
}
}
public static void editPlaylist(Context context, Playlist playlist,
@Nullable List<Song> songs) {
ignoreSingleContentUpdate();
// Clear the playlist...
Uri uri = MediaStore.Audio.Playlists.Members
.getContentUri("external", playlist.getPlaylistId());
ContentResolver resolver = context.getContentResolver();
resolver.delete(uri, null, null);
if (songs != null) {
// ... Then add all of the songs to it
ContentValues[] values = new ContentValues[songs.size()];
for (int i = 0; i < songs.size(); i++) {
values[i] = new ContentValues();
values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i + 1);
values[i].put(
MediaStore.Audio.Playlists.Members.AUDIO_ID,
songs.get(i).getSongId());
}
ignoreSingleContentUpdate();
resolver.bulkInsert(uri, values);
ignoreSingleContentUpdate();
resolver.notifyChange(Uri.parse("content://media"), null);
}
}
public static void cleanUpLists(Context context, SQLiteDatabase db, Account[] accounts, String authority)
{
// make a list of the accounts array
List<Account> accountList = Arrays.asList(accounts);
db.beginTransaction();
try
{
Cursor c = db.query(Tables.LISTS, new String[] { TaskListColumns._ID, TaskListSyncColumns.ACCOUNT_NAME, TaskListSyncColumns.ACCOUNT_TYPE }, null,
null, null, null, null);
// build a list of all task list ids that no longer have an account
List<Long> obsoleteLists = new ArrayList<Long>();
try
{
while (c.moveToNext())
{
String accountType = c.getString(2);
// mark list for removal if it is non-local and the account
// is not in accountList
if (!TaskContract.LOCAL_ACCOUNT_TYPE.equals(accountType))
{
Account account = new Account(c.getString(1), accountType);
if (!accountList.contains(account))
{
obsoleteLists.add(c.getLong(0));
// remove syncstate for this account right away
db.delete(Tables.SYNCSTATE, SyncState.ACCOUNT_NAME + "=? and " + SyncState.ACCOUNT_TYPE + "=?", new String[] { account.name,
account.type });
}
}
}
}
finally
{
c.close();
}
if (obsoleteLists.size() == 0)
{
// nothing to do here
return;
}
// remove all accounts in the list
for (Long id : obsoleteLists)
{
if (id != null)
{
db.delete(Tables.LISTS, TaskListColumns._ID + "=" + id, null);
}
}
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}
// notify all observers
ContentResolver cr = context.getContentResolver();
cr.notifyChange(TaskLists.getContentUri(authority), null);
cr.notifyChange(Tasks.getContentUri(authority), null);
cr.notifyChange(Instances.getContentUri(authority), null);
Utils.sendActionProviderChangedBroadCast(context, authority);
}
public static void deleteAlbumArt(@NonNull Context context, int albumId) {
ContentResolver contentResolver = context.getContentResolver();
Uri localUri = Uri.parse("content://media/external/audio/albumart");
contentResolver.delete(ContentUris.withAppendedId(localUri, albumId), null, null);
contentResolver.notifyChange(localUri, null);
}
@Override
public void onMountedServerChange() {
final Uri rootsUri = DocumentsContract.buildRootsUri(AUTHORITY);
final ContentResolver resolver = getContext().getContentResolver();
resolver.notifyChange(rootsUri, null, false);
}
public static void cleanUpLists(Context context, SQLiteDatabase db, Account[] accounts, String authority)
{
// make a list of the accounts array
List<Account> accountList = Arrays.asList(accounts);
db.beginTransaction();
try
{
Cursor c = db.query(Tables.LISTS, new String[] { TaskListColumns._ID, TaskListSyncColumns.ACCOUNT_NAME, TaskListSyncColumns.ACCOUNT_TYPE }, null,
null, null, null, null);
// build a list of all task list ids that no longer have an account
List<Long> obsoleteLists = new ArrayList<Long>();
try
{
while (c.moveToNext())
{
String accountType = c.getString(2);
// mark list for removal if it is non-local and the account
// is not in accountList
if (!TaskContract.LOCAL_ACCOUNT_TYPE.equals(accountType))
{
Account account = new Account(c.getString(1), accountType);
if (!accountList.contains(account))
{
obsoleteLists.add(c.getLong(0));
// remove syncstate for this account right away
db.delete(Tables.SYNCSTATE, SyncState.ACCOUNT_NAME + "=? and " + SyncState.ACCOUNT_TYPE + "=?", new String[] {
account.name,
account.type });
}
}
}
}
finally
{
c.close();
}
if (obsoleteLists.size() == 0)
{
// nothing to do here
return;
}
// remove all accounts in the list
for (Long id : obsoleteLists)
{
if (id != null)
{
db.delete(Tables.LISTS, TaskListColumns._ID + "=" + id, null);
}
}
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}
// notify all observers
ContentResolver cr = context.getContentResolver();
cr.notifyChange(TaskLists.getContentUri(authority), null);
cr.notifyChange(Tasks.getContentUri(authority), null);
cr.notifyChange(Instances.getContentUri(authority), null);
Utils.sendActionProviderChangedBroadCast(context, authority);
}
@Override
public int update(Uri url, ContentValues values, String where,
String[] whereArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int match = URI_MATCHER.match(url);
if (match == -1 || match == URI_MATCH_SUGGEST) {
throw new IllegalArgumentException("Unknown URL");
}
if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
StringBuilder sb = new StringBuilder();
if (where != null && where.length() > 0) {
sb.append("( ");
sb.append(where);
sb.append(" ) AND ");
}
String id = url.getPathSegments().get(1);
sb.append("_id = ");
sb.append(id);
where = sb.toString();
}
ContentResolver cr = getContext().getContentResolver();
// Not all bookmark-table updates should be backed up. Look to see
// whether we changed the title, url, or "is a bookmark" state, and
// request a backup if so.
if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) {
boolean changingBookmarks = false;
// Alterations to the bookmark field inherently change the bookmark
// set, so we don't need to query the record; we know a priori that
// we will need to back up this change.
if (values.containsKey(BookmarkColumns.BOOKMARK)) {
changingBookmarks = true;
} else if ((values.containsKey(BookmarkColumns.TITLE)
|| values.containsKey(BookmarkColumns.URL))
&& values.containsKey(BookmarkColumns._ID)) {
// If a title or URL has been changed, check to see if it is to
// a bookmark. The ID should have been included in the update,
// so use it.
Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
new String[] { BookmarkColumns.BOOKMARK },
BookmarkColumns._ID + " = "
+ values.getAsString(BookmarkColumns._ID), null, null);
if (cursor.moveToNext()) {
changingBookmarks = (cursor.getInt(0) != 0);
}
cursor.close();
}
// if this *is* a bookmark row we're altering, we need to back it up.
if (changingBookmarks) {
mBackupManager.dataChanged();
}
}
int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
cr.notifyChange(url, null);
return ret;
}
/**
* Send notification that we've deleted the {@link AppWidgetHost},
* probably as part of the initial database creation. The receiver may
* want to re-call {@link AppWidgetHost#startListening()} to ensure
* callbacks are correctly set.
*/
private void sendAppWidgetResetNotify() {
final ContentResolver resolver = mContext.getContentResolver();
resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
}
/**
* Send notification that we've deleted the {@link AppWidgetHost},
* probably as part of the initial database creation. The receiver may
* want to re-call {@link AppWidgetHost#startListening()} to ensure
* callbacks are correctly set.
*/
private void sendAppWidgetResetNotify() {
final ContentResolver resolver = mContext.getContentResolver();
resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
}