Is there any way to detect whether MediaScanner is running now or not .
for example if mediascanner is running,Thread will sleep for 200 milis.
thanks.
Use below code.
public static boolean isMediaScannerScanning(ContentResolver cr) {
boolean result = false;
Cursor cursor = query(cr, MediaStore.getMediaScannerUri(),
new String [] {MediaStore.MEDIA_SCANNER_VOLUME},
null, null, null);
if (cursor != null) {
if (cursor.getCount() == 1) {
cursor.moveToFirst();
result = "external".equals(cursor.getString(0));
}
cursor.close();
}
return result;
}
It is copied from ImageManager.java of AOSP.
Related
I have an application that runs under system account and one of the features is create custom APNs depending the client's configurations.
The code below works fine, but just create the APN and set it as default for simcard 1. O would like to set it depending on simcards if the software is running on Android 5 or above. I could not find any documentation about it. Anyone knows something else about APNs?
public static int createNewApn(Context context, APN apn, boolean setAsDefaultAPN) {
int apnid = -1;
try {
if (apn != null) {
Uri APN_URI = Uri.parse("content://telephony/carriers");
ContentResolver resolver = context.getContentResolver();
ContentValues values = prepareValues(apn);
Cursor c = null;
Uri newRow = resolver.insert(APN_URI, values);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
int tableIndex = c.getColumnIndex("_id");
c.moveToFirst();
apnid = c.getShort(tableIndex);
}
if (c != null) {
c.close();
}
if (apnid > -1 && setAsDefaultAPN) {
ContentValues v = new ContentValues(1);
v.put("apn_id", apnid);
context.getContentResolver().update(APN_PREFER_URI, v, null, null);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return apnid;
}
I am trying to check whether given name is in phone contact or not, but it always returns false, i cant figure out what mistake i have done
public boolean ContactNotFound(String no, String name) {
if (no != null) {
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI, Uri.encode(name));
String[] mPhoneNumberProjection = { PhoneLookup._ID,
PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = con.getContentResolver().query(lookupUri,
mPhoneNumberProjection, null, null, null);
LogUtil.d("Count -->" + cur.getCount());
if (cur.getCount() > 0) {
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
} else {
return false;
}
} else {
return false;
}
}
Kindly help me to figure out the issue
I suggest you to save all your contact names in arraylist temporarily and then check name using arrayList.contains(name) method
Hi i wants to count the total number of columns in android sqlite table, please guide that how to achieve? what i tried is below
public int numberOfColumns(String tableName) {
Cursor cursor = database.query(tableName, null, null, null, null, null, null);
if (cursor != null) {
if (cursor.getColumnCount() > 0) {
return cursor.getColumnCount();
} else {
return 0;
}
} else {
return 0;
}
}
this works fine, what i wants to know is about it is, this approach is fine or anything better i can do!!
i visited lot of stackoverflow links but still have confusion.
This works. There are some other methods such as PRAGMA table_info(tablename) but they aren't really that much better.
Checking for cursor != null is not necessary.
Also, it's a good idea to close your cursor before exiting the method.
You need to close your cursor after leaving the function, this change should work (not tested)
public int numberOfColumns(String tableName) {
int result = 0;
Cursor cursor = null;
try {
cursor = database.query(tableName, null, null, null, null, null,
null);
result = cursor.getColumnCount();
if (result < 0) {
result = 0;
}
} finally {
if (cursor != null)
cursor.close();
}
return result;
}
i have a cursor that I am using in the following code. But I want to close the cursor after it is used and no longer needed. The problem is that the cursor is used in the return statement, but I can't close it after the return statement because that is unreachable code. It is used in the return statement so I can't close it above that line. How do I close the cursor? This is not like the old managedQuery, I assume that you have to close it.
public String getPath(Uri uri) {
String[] projection = { MediaStore.Audio.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
// cursor.close() <--- not possible because it is unreachable code after return
}
You can assign it to a String object then close cursor and return it.
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
Would using a finally block work for you? I'm not experienced in your particular problem but could you put your cleanup in the finally block that would otherwise be bypassed by the return statement:
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
Just an idea! Don't shoot me if not!
Try - Finally to the rescue...
public String getPath(Uri uri) {
Cursor cursor = null;
try {
String[] projection = { MediaStore.Audio.Media.DATA };
cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
finally {
if (cursor != null)
cursor.close(); // close the cursor
}
}
NOTE
Just noted another answer below..
If you're inside an activity, you can call startManaginCursor() and pass the cursor instance so Android will manage its lifetime. If you don't have access to Android Application Context (ex: code is in a utility class) use above method. (and sprinkle a bit of some error handling ;) )
if (this.daoDatabase != null) {
final Cursor genericCursor = this.daoDatabase.query(this.tableName, null, null, null, null, null, orderByCriteria);
if (genericCursor != null) {
if (genericCursor.moveToFirst()) {
do {
// do stuff...
} while (genericCursor.moveToNext());
}
genericCursor.close();
}
}
I have phone number. Is there any way to check whether the phone number exists in contacts database in the device or not? Depending on that I need have move further in my app. Please suggest or if any one can have sample code snippet please provide.
The below is the code I wrote:
public boolean contactExists(Activity _activity, String number) {
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = _activity.getContentResolver().query(number, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}// contactExists
Thanks in Advance...
public boolean contactExists(Activity _activity, String number) {
if (number != null) {
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
} else {
return false;
}
}// contactExists
Handled nullpointer exception.
A minor change in your code ::
You need to have lookupUri..
public boolean contactExists(Activity _activity, String number) {
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}// contactExists
I tried the code above on an ice cream device (SIII) and it didnt work
so after some search i ended up creating this method (which is working nicely )
private boolean isContact(String incommingNumber) {
Cursor cursor =null;
String name = null;
try {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incommingNumber));
cursor = MainService.this.getContentResolver().query(uri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
} finally {
if(cursor!=null){
cursor.close();
}
}
return Util.hasValue(name);
}