本文整理汇总了Java中android.content.ContentResolver.query方法的典型用法代码示例。如果您正苦于以下问题:Java ContentResolver.query方法的具体用法?Java ContentResolver.query怎么用?Java ContentResolver.query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.content.ContentResolver
的用法示例。
在下文中一共展示了ContentResolver.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: queryForLong
import android.content.ContentResolver; //导入方法依赖的package包/类
private static long queryForLong(Context context, Uri self, String column,
long defaultValue) {
final ContentResolver resolver = context.getContentResolver();
Cursor c = null;
try {
c = resolver.query(self, new String[] { column }, null, null, null);
if (c.moveToFirst() && !c.isNull(0)) {
return c.getLong(0);
} else {
return defaultValue;
}
} catch (Exception e) {
Log.w(TAG, "Failed query: " + e);
return defaultValue;
} finally {
closeQuietly(c);
}
}
示例2: onClick
import android.content.ContentResolver; //导入方法依赖的package包/类
public void onClick( View v )
{
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null);
int indexBody = cursor.getColumnIndex( SmsReceiver.BODY );
int indexAddr = cursor.getColumnIndex( SmsReceiver.ADDRESS );
if ( indexBody < 0 || !cursor.moveToFirst() ) return;
smsList.clear();
do
{
String str = "Sender: " + cursor.getString( indexAddr ) + "\n" + cursor.getString( indexBody );
smsList.add( str );
}
while( cursor.moveToNext() );
ListView smsListView = (ListView) findViewById( R.id.SMSList );
smsListView.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, smsList) );
smsListView.setOnItemClickListener( this );
}
示例3: loadArtistList
import android.content.ContentResolver; //导入方法依赖的package包/类
public static ArrayList<Artist> loadArtistList(ContentResolver resolver) {
ArrayList<Artist> list = new ArrayList<>();
Cursor cursor = resolver.query(
MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Audio.Artists._ID,
MediaStore.Audio.Artists.ARTIST,
// MediaStore.Audio.Artists.
},
null, null, MediaStore.Audio.Artists.ARTIST);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Artist artist = new Artist();
artist.setId(cursor.getInt(0));
artist.setName(cursor.getString(1));
list.add(artist);
} while (cursor.moveToNext());
}
cursor.close();
}
return list;
}
示例4: getFileFromMediaUri
import android.content.ContentResolver; //导入方法依赖的package包/类
/**
* 通过Uri获取文件
*
* @param ac
* @param uri
* @return
*/
public static File getFileFromMediaUri(Context ac, Uri uri) {
if (uri.getScheme().toString().compareTo("content") == 0) {
ContentResolver cr = ac.getContentResolver();
Cursor cursor = cr.query(uri, null, null, null, null);// 根据Uri从数据库中找
if (cursor != null) {
cursor.moveToFirst();
String filePath = cursor.getString(cursor.getColumnIndex("_data"));// 获取图片路径
cursor.close();
if (filePath != null) {
return new File(filePath);
}
}
} else if (uri.getScheme().toString().compareTo("file") == 0) {
return new File(uri.toString().replace("file://", ""));
}
return null;
}
示例5: getPath
import android.content.ContentResolver; //导入方法依赖的package包/类
public static String getPath(ContentResolver resolver, Uri uri) {
if (uri == null) {
return null;
}
if (SCHEME_CONTENT.equals(uri.getScheme())) {
Cursor cursor = null;
try {
cursor = resolver.query(uri, new String[]{MediaStore.Images.ImageColumns.DATA},
null, null, null);
if (cursor == null || !cursor.moveToFirst()) {
return null;
}
return cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
} finally {
if (cursor != null) {
cursor.close();
}
}
}
return uri.getPath();
}
示例6: getALlPlalists
import android.content.ContentResolver; //导入方法依赖的package包/类
public static ArrayList<String[]> getALlPlalists(ContentResolver Cr){
ArrayList<String[]> list = new ArrayList<String[]>();
Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
String[] cols = new String[] {MediaStore.Audio.Playlists.NAME, MediaStore.Audio.Playlists._ID};
Cursor DataCursor = Cr.query( uri,cols, null,null, MediaStore.Audio.Playlists.NAME +" COLLATE NOCASE ASC");// +" COLLATE NOCASE ASC");
int length = DataCursor.getCount();
for (int i = 0; i < length; i++) {
DataCursor.moveToNext();
String str = DataCursor.getString(0);
if(!str.startsWith(" ") && str.length() != 0){
list.add(new String[] {str,DataCursor.getInt(1)+""});
}
}
DataCursor.close();
return list;
}
示例7: enabledAlarms
import android.content.ContentResolver; //导入方法依赖的package包/类
private List<Alarm> enabledAlarms(ContentResolver r) {
LinkedList<Alarm> ids = new LinkedList<Alarm>();
Cursor c = r.query(
AlarmClockProvider.ALARMS_URI,
new String[] {
AlarmClockProvider.AlarmEntry._ID,
AlarmClockProvider.AlarmEntry.TIME,
AlarmClockProvider.AlarmEntry.DAY_OF_WEEK,
AlarmClockProvider.AlarmEntry.NEXT_SNOOZE },
AlarmClockProvider.AlarmEntry.ENABLED + " == 1",
null, null);
while (c.moveToNext())
ids.add(new Alarm(c));
c.close();
return ids;
}
示例8: makeArtistSongCursor
import android.content.ContentResolver; //导入方法依赖的package包/类
public static Cursor makeArtistSongCursor(Context context, long artistID) {
ContentResolver contentResolver = context.getContentResolver();
final String artistSongSortOrder = PreferencesUtility.getInstance(context).getArtistSongSortOrder();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String string = "is_music=1 AND title != '' AND artist_id=" + artistID;
return contentResolver.query(uri, new String[]{"_id", "title", "artist", "album", "duration", "track", "album_id"}, string, null, artistSongSortOrder);
}
示例9: getMaxId
import android.content.ContentResolver; //导入方法依赖的package包/类
/** helper to find the max of _id (=newest in android's db) in our db */
private static String getMaxId (ContentResolver cr) {
Cursor c = cr.query(MusicStoreInternal.FILES_IMPORT, MAX_ID_PROJ, null, null, null);
String result = null;
if (c != null) {
if (c.moveToFirst()) {
result = c.getString(0);
}
c.close();
}
return TextUtils.isEmpty(result) ? "0" : result;
}
示例10: getFromMediaUri
import android.content.ContentResolver; //导入方法依赖的package包/类
@Nullable
public static File getFromMediaUri(Context context, ContentResolver resolver, Uri uri) {
if (uri == null) return null;
if (SCHEME_FILE.equals(uri.getScheme())) {
return new File(uri.getPath());
} else if (SCHEME_CONTENT.equals(uri.getScheme())) {
final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
Cursor cursor = null;
try {
cursor = resolver.query(uri, filePathColumn, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d")) ?
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME) :
cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
// Picasa images on API 13+
if (columnIndex != -1) {
String filePath = cursor.getString(columnIndex);
if (!TextUtils.isEmpty(filePath)) {
return new File(filePath);
}
}
}
} catch (IllegalArgumentException e) {
// Google Drive images
return getFromMediaUriPfd(context, resolver, uri);
} catch (SecurityException ignored) {
// Nothing we can do
} finally {
if (cursor != null) cursor.close();
}
}
return null;
}
示例11: query
import android.content.ContentResolver; //导入方法依赖的package包/类
private static Cursor query(Context context, Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
try {
ContentResolver resolver = context.getContentResolver();
if (resolver == null) {
return null;
}
return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
} catch (UnsupportedOperationException ex) {
return null;
}
}
示例12: exists
import android.content.ContentResolver; //导入方法依赖的package包/类
public static boolean exists(Context context, Uri self) {
final ContentResolver resolver = context.getContentResolver();
Cursor c = null;
try {
c = resolver.query(self, new String[] {
DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
return c.getCount() > 0;
} catch (Exception e) {
Log.w(TAG, "Failed query: " + e);
return false;
} finally {
closeQuietly(c);
}
}
示例13: getMaxLocalId
import android.content.ContentResolver; //导入方法依赖的package包/类
private static String getMaxLocalId (ContentResolver cr) {
Cursor c = cr.query(MusicStoreInternal.FILES_IMPORT, MAX_LOCAL_PROJ, null, null, null);
String result = null;
if (c != null) {
if (c.moveToFirst()) {
result = c.getString(0);
}
c.close();
}
return TextUtils.isEmpty(result) ? "0" : result;
}
示例14: checkWriteContacts
import android.content.ContentResolver; //导入方法依赖的package包/类
/**
* write and delete contacts info, {@link Manifest.permission#WRITE_CONTACTS}
* and we should get read contacts permission first.
*
* @param activity
* @return true if success
* @throws Exception
*/
private static boolean checkWriteContacts(Context activity) throws Exception {
if (checkReadContacts(activity)) {
// write some info
ContentValues values = new ContentValues();
ContentResolver contentResolver = activity.getContentResolver();
Uri rawContactUri = contentResolver.insert(ContactsContract.RawContacts
.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds
.StructuredName.CONTENT_ITEM_TYPE);
values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, TAG);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, TAG_NUMBER);
contentResolver.insert(ContactsContract.Data.CONTENT_URI, values);
// delete info
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = activity.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Contacts.Data._ID},
"display_name=?", new String[]{TAG}, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int id = cursor.getInt(0);
resolver.delete(uri, "display_name=?", new String[]{TAG});
uri = Uri.parse("content://com.android.contacts/data");
resolver.delete(uri, "raw_contact_id=?", new String[]{id + ""});
}
cursor.close();
}
return true;
} else {
return false;
}
}
示例15: testCanUpdate
import android.content.ContentResolver; //导入方法依赖的package包/类
@Test
public void testCanUpdate() {
insertApp("not installed", "not installed");
insertAndInstallApp("installed, only one version available", 1, 1, false, 0);
insertAndInstallApp("installed, already latest, no ignore", 10, 10, false, 0);
insertAndInstallApp("installed, already latest, ignore all", 10, 10, true, 0);
insertAndInstallApp("installed, already latest, ignore latest", 10, 10, false, 10);
insertAndInstallApp("installed, already latest, ignore old", 10, 10, false, 5);
insertAndInstallApp("installed, old version, no ignore", 5, 10, false, 0);
insertAndInstallApp("installed, old version, ignore all", 5, 10, true, 0);
insertAndInstallApp("installed, old version, ignore latest", 5, 10, false, 10);
insertAndInstallApp("installed, old version, ignore newer, but not latest", 5, 10, false, 8);
ContentResolver r = context.getContentResolver();
// Can't "update", although can "install"...
App notInstalled = AppProvider.Helper.findSpecificApp(r, "not installed", 1, Cols.ALL);
assertFalse(notInstalled.canAndWantToUpdate(context));
assertResultCount(contentResolver, 2, AppProvider.getCanUpdateUri(), PROJ);
assertResultCount(contentResolver, 9, AppProvider.getInstalledUri(), PROJ);
App installedOnlyOneVersionAvailable = AppProvider.Helper.findSpecificApp(r, "installed, only one version available", 1, Cols.ALL);
App installedAlreadyLatestNoIgnore = AppProvider.Helper.findSpecificApp(r, "installed, already latest, no ignore", 1, Cols.ALL);
App installedAlreadyLatestIgnoreAll = AppProvider.Helper.findSpecificApp(r, "installed, already latest, ignore all", 1, Cols.ALL);
App installedAlreadyLatestIgnoreLatest = AppProvider.Helper.findSpecificApp(r, "installed, already latest, ignore latest", 1, Cols.ALL);
App installedAlreadyLatestIgnoreOld = AppProvider.Helper.findSpecificApp(r, "installed, already latest, ignore old", 1, Cols.ALL);
assertFalse(installedOnlyOneVersionAvailable.canAndWantToUpdate(context));
assertFalse(installedAlreadyLatestNoIgnore.canAndWantToUpdate(context));
assertFalse(installedAlreadyLatestIgnoreAll.canAndWantToUpdate(context));
assertFalse(installedAlreadyLatestIgnoreLatest.canAndWantToUpdate(context));
assertFalse(installedAlreadyLatestIgnoreOld.canAndWantToUpdate(context));
App installedOldNoIgnore = AppProvider.Helper.findSpecificApp(r, "installed, old version, no ignore", 1, Cols.ALL);
App installedOldIgnoreAll = AppProvider.Helper.findSpecificApp(r, "installed, old version, ignore all", 1, Cols.ALL);
App installedOldIgnoreLatest = AppProvider.Helper.findSpecificApp(r, "installed, old version, ignore latest", 1, Cols.ALL);
App installedOldIgnoreNewerNotLatest = AppProvider.Helper.findSpecificApp(r, "installed, old version, ignore newer, but not latest", 1, Cols.ALL);
assertTrue(installedOldNoIgnore.canAndWantToUpdate(context));
assertFalse(installedOldIgnoreAll.canAndWantToUpdate(context));
assertFalse(installedOldIgnoreLatest.canAndWantToUpdate(context));
assertTrue(installedOldIgnoreNewerNotLatest.canAndWantToUpdate(context));
Cursor canUpdateCursor = r.query(AppProvider.getCanUpdateUri(), Cols.ALL, null, null, null);
assertNotNull(canUpdateCursor);
canUpdateCursor.moveToFirst();
List<String> canUpdateIds = new ArrayList<>(canUpdateCursor.getCount());
while (!canUpdateCursor.isAfterLast()) {
canUpdateIds.add(new App(canUpdateCursor).packageName);
canUpdateCursor.moveToNext();
}
canUpdateCursor.close();
String[] expectedUpdateableIds = {
"installed, old version, no ignore",
"installed, old version, ignore newer, but not latest",
};
assertContainsOnly(expectedUpdateableIds, canUpdateIds);
}