Android can't read blocked numbers anymore - android

I know similar question has been asked before,
my code was working before, and now it does not, my app is the default dialer and is also a system app,
the canCurrentUserBlockNumbers(this) returns true, however always an empty cursor is returned now,
any suggestion?
Thank you
if (canCurrentUserBlockNumbers(this))
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show(); // I see this "OK" Toast
else
Toast.makeText(this, "KO", Toast.LENGTH_SHORT).show();
Cursor c = getContentResolver().query(BlockedNumberContract.BlockedNumbers.CONTENT_URI,
new String[]{BlockedNumberContract.BlockedNumbers.COLUMN_ID, BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER,
BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER}, null, null, null);
TextView tv = findViewById(R.id.textviewblockednumbers);
tv.setText("ciao");
//int numElements=(c.getColumnCount();
while (c.moveToNext()) {
String name = c.getString(c.getColumnIndexOrThrow(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER));
tv.append(name);
tv.append(",");
c.close();
}
}

update for everyone interested,
with another phone of the same brand (Xiaomi) the cursor is not empty and I can read blocked numbers,
looks like a platform trouble

Related

ContentResolver().update doesn't work

I'm new to android programming. I tried to update a task in my database with the ContentResolver. But instead of updating and/or showing the toast message, the app seems to process for a while (screen is white for a few seconds) and the item remains the same.
Other operations on ContentResolver works (e.g. inserting)
I tried to debug, but I can't find an error in the debugging window. Obviously all values and Uri seems to be correct... Error must be in following line:
int rowsAffected = getContentResolver().update(mCurrentTaskUri, values, null, null);
if (rowsAffected == 0) {
Toast.makeText(this, getString(R.string.aufgabe_bearbeiten_fehlgeschlagen), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, getString(R.string.aufgabe_bearbeiten_erfolgreich), Toast.LENGTH_SHORT).show();
}

Android M & Lollipop - Mark missed call as read

I use below code to clear missed calls after I launch my app. In this I get rows affeted is 1. But when i get next missed call, at that time android's stock phone app adds a new notification as "2 new missed calls". Means they are not counting my clear. Am I missing something.
Note: If i launch the stock phone app once, the counter is reset to 0 again.
public boolean markMissedCallsAsRead() {
ContentValues values = new ContentValues();
values.put(CallLog.Calls.NEW, Integer.valueOf(0));
values.put(CallLog.Calls.IS_READ, Integer.valueOf(1));
StringBuilder where = new StringBuilder();
where.append(CallLog.Calls.NEW);
where.append(" = 1 AND ");
//where.append(CallLog.Calls.IS_READ).append(" = 0");
//where.append(" AND ");
where.append(CallLog.Calls.TYPE).append(" = ").append(CallLog.Calls.MISSED_TYPE);
int rows = context.getContentResolver().update(CallLog.Calls.CONTENT_URI, values, where.toString(),
null);
Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, "cleared call logs " + rows);
return true;
}
I'm facing the same problem. There is a similar question here:
https://stackoverflow.com/a/26564121/6433463
I'm starting to think (as stated in that thread) that the only way to achieve that is by opening the stock call log (I hope I'm wrong, but couldn't find anything else).

Retrieve browser history records in all the browser apps in android?

I want to get all the browser history records from different browsers in android mobile phone ?(Maybe as you know, there are usually more than one browser apps in a phone ).Is there anyone has done this successfully and give me a hint ? Example code is preferred.Any help will be helpful.. Thanks a lot in advance ! :)
Just look at this. I used it in my code and I am getting browser history through it(Default Browser).
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String selection = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
mCursor = this.managedQuery(Browser.BOOKMARKS_URI, proj, selection, null, null);
this.startManagingCursor(mCursor);
mCursor.moveToFirst();
String title = "";
String url = "";
if (mCursor.moveToFirst() && mCursor.getCount() > 0) {
while (mCursor.isAfterLast() == false && cont) {
title = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCursor.moveToNext();
}
}
Hope this helps you.
Limitations :
Browser.BOOKMARKS_URI will, at most, work for the open source Browser app that is part of the Android Open Source Project. Device manufacturers are welcome to replace that app with something else that will not be recording its history, bookmarks, or anything else in that ContentProvider. Similarly, users are allowed to download third-party browsers, which may not be storing things in that ContentProvider.

Android Cursor - moveToNext() doesnt seem to go to end of cursor

Why doesnt my Android Cursor go all the way to the end of the original "promise"??
My cursor.getCount() differs from my last cursor.getPosition(). Check my while loop! It is all I do with it!
Notes:
1. it is about querying the Contacts content provider (android api >5)
2. I display only the esential code
Cursor cursor = mContext.getContentResolver().query(mUri, mProjections, null, null, null);
Logger.d(TAG, "*** cursor.getCount(): "+cursor.getCount());
while (cursor.moveToNext()) {
Logger.d(TAG, "| position: "+cursor.getPosition());
processMainCursor(serializer, cursor);
}
cursor.close();
processMainCursor() will display data from cursor + do another queries: one 4 phones, one 4 emails, one 4 IM accounts:
void processMainCursor(XmlSerializer serializer, Cursor main_cursor) {
writeCursorData(serializer, main_cursor); //writes cursor data, column by column
writePhoneEntities(serializer, main_cursor);
writeEmailEntities(serializer, main_cursor);
writeIMEntities(serializer, main_cursor);
}
In none of my writeXXX methods do i close my main_cursor or move next!!!..have to trust me on that.. i just do a new query, print data & close that cursor
So statistics:
cursor.getCount() = 695 (always)
commenting writePhoneEntities, writeEmailEntities, writeIMEntities: cursor.getCount() =
last cursor.getPosition() = 695 (so correct!)
leaving one/two/all of my writeXEntities shows randomness; example: leaving them all:
last cursor.getPosition() sometimes displays 254, 257, 253, etc; leaving just phone & IM: 514, 510, 511, etc (so different RUN -> different last cursor.getPosition() VALUE)
So oppinions.. Why is that? Is it memory related?
Update:
Leaving any of my writeXEntities displays at the end in logcat:
Removing dead content provider: contacts
Update 2
Adding cursor.moveToFirst(); & doing loop like
do {
//do whatever you want
} while (cursor.moveToNext());
didn't do the job..
So maybe the answer is in this logcat entries:
05-21 23:29:30.209: I/ActivityThread(7085): Removing dead content provider: contacts
05-21 23:29:30.209: I/ActivityThread(7085): Removing dead content provider: com.android.contacts
SAMPLE OF a writeXEntity REMOVED
SOLUTION .. i wasnt closing the cursors from writeXEntity corectly (probably leaving quite a lot of open cursor after main while)
in reality i was closing like this
if(phone_cursor!=null && phone_cursor.getCount() > 0)
{
//... stuff
phone_cursor.close();
}
i should have closed after if
if(phone_cursor!=null && phone_cursor.getCount() > 0)
{
//... stuff
}
phone_cursor.close();
I guess leaving a basilion cursor open ..was the answer?!?
You need to move the cursor to the first row. Try adding cur.moveToFirst() before the while loop.
You might also consider using a do-while loop. This will ensure that you never skip over the first row in the cursor:
if (cursor.moveToFirst()) {
do {
//do whatever you want
} while (cursor.moveToNext());
}
cursor.close();
Well, they won't be the same number as the getCount is the number of items, and the position is the position, ( the first one being 0). So the final position should always be one less than the count.
If it's something other than that, I guess I'm not understanding your question properly.
Use cursor as shown below:
if(cursor.getCount() == 0)
{
//No entry found
}
else {
cursor.moveToFirst();
do {
//do whatever you want
} while (cursor.moveToNext());
cursor.close();
After reading the answer you already found (problem with closing cursors) I think that the best way to ensure you close them all is with this code:
Cursor c = null;
try {
c = <your query>;
if (c.moveToFirst()) { // No point in doing more if empty.
do {
<process this cursor row>
} while (c.moveToNext());
}
}
finally {
if (c != null) c.close(); // Not sure the check needed, maybe if query was really wrong.
}

Android nullpointerexception at .indexOf()

I have an app on the market, but in some cases it force closes at the open. According to the crash error there is something wrong with the indexOf command (NullPointerException). Part of the code:
contactName = null;
Context context = getApplicationContext();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
myArr.add("");
while (cursor.moveToNext())
{
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
spaceIndex = contactName.indexOf(' '); //this is the bad row
spaceLastIndex = contactName.lastIndexOf(' ');
myArr.add(contactName);
}
I test my app on 3 different phones, app is working fine. So I cannot test the code if I change something, as I could not tell the difference. What can cause the error and why does it emerge on only a few phones? (5% of the downloads). A guy contacted me with this error, there are a couple of contacts with special characters in his phone (HTC Legend CM 7.1, and a Vodafone 845 Android 2.1). So I added the same characters to a contact of mine, put blank spaces before the name, tried everything to mess with the contact name, app runs smoothly, so this is not the problem. I am out of the options.
You should check the contactName, if it isn't null. I think that the column, from which are you trying to get the value, is just empty.
if (contactName != null) { ... and so on
contactName is clearly coming back null. You need some sort of null check before you call indexOf on it.
// snip
while (cursor.moveToNext())
{
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (contactName == null) continue;
spaceIndex = contactName.indexOf(' ');
spaceLastIndex = contactName.lastIndexOf(' ');
myArr.add(contactName);
}
contactName = cursor.getString(cursor.getColumnIndexorThrow(ContactsContract.Contacts.DISPLAY_NAME));
Even the display name can be null for some cases... say blank contact..
so check whether the contactName is null or not first before getting index from it.

Categories

Resources