Is there a way how to get data log like you can get sms or call log?
ContentResolver contentResolver = context.getContentResolver();
Cursor c = contentResolver.query(CallLog.Calls.CONTENT_URI, null,
null, null, CallLog.Calls.DATE + " DESC");
int number = c.getColumnIndex(CallLog.Calls.NUMBER);
int type = c.getColumnIndex(CallLog.Calls.TYPE);
int date = c.getColumnIndex(CallLog.Calls.DATE);
int duration = c.getColumnIndex(CallLog.Calls.DURATION);
I need to know how many data I used by mobile connection (no wifi) last month to count with that in future.
Related
I am trying to add Events in my app using a calender.According to CalenderContract,I need to provide a constant ID each time I add an event to the calender.I don't know how to do that.
I tried using calender_ID =1 which worked on some devices and calender_ID = 3 which also worked on some devices.
I think there would be some default ID which can be used to make this work properly.
Can anyone please tell me how this can be done?
Thanks in Advance.
you can get the calender id by this following code :
String projection[] = {"_id", "calendar_displayName"};
Uri calendars;
calendars = Uri.parse("content://com.android.calendar/calendars");
ContentResolver contentResolver = c.getContentResolver();
Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);
if (managedCursor.moveToFirst()){
m_calendars = new MyCalendar[managedCursor.getCount()];
String calName;
String calID;
int cont= 0;
int nameCol = managedCursor.getColumnIndex(projection[1]);
int idCol = managedCursor.getColumnIndex(projection[0]);
do {
calName = managedCursor.getString(nameCol);
calID = managedCursor.getString(idCol);
m_calendars[cont] = new MyCalendar(calName, calID);
cont++;
} while(managedCursor.moveToNext());
managedCursor.close();
}
So you can get the calender Id at runtime and use it . you don't need hardcoded 1 or 3 column Id.
I was trying to get call logs from android device by using the code from here.
Cursor c = managedQuery(allCalls, null, null, null, null);
String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));//
and getting an exception as:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 500
Can anyone help me on this?
Thanks
Can you try this?
Pass new String[]{"*"} as a projection instead of null.
Cursor c = managedQuery(allCalls, new String[]{"*"}, null, null, null);
I got the answer after trying this code
Cursor c = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, null);
int durationIndex = c.getColumnIndex(CallLog.Calls.DURATION);
String duration="";
while (c.moveToNext()) {
duration = c.getString(durationIndex);
}
number and name is not required for me, but we can get that too using the same way.
I've been trying to figure out how to develop a requirement of one of my projects. The requirement is to get information about phone calls:
Timestamp and phone number of ingoing/outgoing calls
Timestamp and reason of finished call (call ended by user, network unreachable...)
What I found until now is not so much. This next link
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
Talks about creating a listener to get incoming calls phone number.
Anyone can help me with the rest of functionality? where to get the outgoing information of phone calls? Is there any way to know when a phone call is finished, and the reason?
Thank you in advance,
private void getCallLogDetail( Context context ) {
String[] projection = new String[] {
BaseColumns._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.TYPE,
CallLog.Calls.DURATION
};
ContentResolver resolver = context.getContentResolver();
Cursor cur = resolver.query(
CallLog.Calls.CONTENT_URI,
projection,
null,
null,
CallLog.Calls.DEFAULT_SORT_ORDER );
if( !cur.isAfterLast()) {
int numberColumn = cur.getColumnIndex( CallLog.Calls.NUMBER );
int typeColumn = cur.getColumnIndex( CallLog.Calls.TYPE );
int durationcolumn = cur.getColumnIndex(CallLog.Calls.DURATION);
String number = cur.getString( numberColumn );
String type = cur.getString( typeColumn );
String duration = cur.getString(durationcolumn);
cur.moveToNext();
}
}
I'm trying to use the following code to grab a random mobile phone number from the contacts:
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + "NAME" + "'", null, null);
cursor.moveToFirst();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contactId, null, null);
List numbers = new ArrayList();
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_MOBILE:
numbers.add(number);
break;
}
}
Random randGen = new Random();
return (String) numbers.get(randGen.nextInt(numbers.size()));
However, running this code produces a crash on line 4, with a message saying "CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0". The crash seems to be caused by the cursor.getString() method. Does anyone know where I'm going wrong? This is using the ContactsContract in Android 2.1. Eclipse gives no errors.
Thanks!
The moveToFirst() method returns a boolean. It returns true if it was able to move to the first row and false otherwise, indicating that the query returned an empty set.
When using a cursor, you should follow something like:
if (cursor.moveToFirst()) {
do {
// do some stuff
} while (cursor.moveToNext());
}
cursor.close();
im using the following code segment to fetch contact names and numbers, it works fine on the emulator, but when i install the app in my Froyo 2.2.1, it just returns me the name, and instead of returning me the number it returns, 'null', can anyone help me solve this issue ?
will greatly appreciate any solution. Thanks
ContentResolver r = getContentResolver();
Cursor cursor = r.query(People.CONTENT_URI, null, null, null, null);
// Let activity manage the cursor
// startManagingCursor(cursor);
// Log.d(TAG, "cursor.getCount()=" + cursor.getCount());
// Get value from content provider
int nameIndex = cursor.getColumnIndex(People.NAME);
int numberIndex = cursor.getColumnIndex(People.NUMBER);//OrThrow(People.NUMBER);
cursor.moveToFirst();
StringBuilder s = new StringBuilder();
do {
String name = cursor.getString(nameIndex);
String number = cursor.getString(numberIndex);
s.append(name+ ": " + number + "\n");
} while (cursor.moveToNext());
The People API is deprecated try using the ContactsContract API which was already introduced for android 2.0+.
You can look at this blog post about using the contactscontract or the api documentation