in my app I should do some action when a call comes but not answered by the user.
I have searched in the android.telephony and the NotificationManager, but I haven't found a method to solve this problem.
Does someone have an idea of how to get to know if there is a missed call on the phone or not ?
Here is code that can query the call log for a missed call. Basically, you will have to trigger this somehow and make sure that you give the call log some time ( a few seconds should do it) to write the information otherwise if you check the call log too soon you will not find the most recent call.
final String[] projection = null;
final String selection = null;
final String[] selectionArgs = null;
final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor cursor = null;
try{
cursor = context.getContentResolver().query(
Uri.parse("content://call_log/calls"),
projection,
selection,
selectionArgs,
sortOrder);
while (cursor.moveToNext()) {
String callLogID = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls._ID));
String callNumber = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
String callDate = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DATE));
String callType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
String isCallNew = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NEW));
if(Integer.parseInt(callType) == MISSED_CALL_TYPE && Integer.parseInt(isCallNew) > 0){
if (_debug) Log.v("Missed Call Found: " + callNumber);
}
}
}catch(Exception ex){
if (_debug) Log.e("ERROR: " + ex.toString());
}finally{
cursor.close();
}
I hope you find this useful.
From what I understand, you need to query the CallLog provider (or maybe CallLog.Calls), and this page explains how to query content provider: http://developer.android.com/guide/topics/providers/content-providers.html#basics
I'd be happy to see the code if you can make this work !
I suppose you have content providers to access call logs.
http://www.anddev.org/video-tut_-_querying_and_displaying_the_calllog-t169.html
http://www.devx.com/wireless/Article/41133
If this code works you just need to run this query at the right time. I mean check some samples that can notify you when you get a call in your device
http://groups.google.com/group/android-developers/browse_thread/thread/d97a759a3708cbe3
Once you get this notification put a timer or use some built in Intents to find that the phone is back to normal state and access the call logs...
Possible duplicate
broadcast receiver for missed call in android
Show Toast on Missed Call in android application
Related
I have a background service that listens to changes to Android SMS content provider. I store the last recorded id of the message that my Service recorded and using it, I want to find the number of messages that were sent when the service was Stopped/Killed.
So when the service is started again and another message is sent, I want to use the timestamp of the last recorded message. I use the below code to do this. My problem is that the cursor always returns 1 record irrespective of how many messages are sent when the service was killed before its restarted.
String COLUMN_NAME_DATE = "date"
String[] projection = new String[]{COLUMN_NAME_ID, COLUMN_NAME_DATE};
Cursor newMessagesCursor = mContext.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_SENT),
projection,
COLUMN_NAME_DATE + "> ? ",
new String[]{String.valueOf(lastMessageTimeStamp)}, null);
int newMessagesCount = (null != newMessagesCursor) ? newMessagesCursor.getCount() : 0;
Any pointers would be helpful. Let me know if there is any alternative approach as well.
Edit: Adding code to retrieve the lastMessageTimeStamp.
Cursor messageCursor = mContext.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_SENT),
projection,
COLUMN_NAME_ID + "= ? ",
new String[]{ lastMessageId}, null);
if(null != messageCursor && messageCursor.moveToFirst()) {
Long lastMessageTimeStamp = Long.parseLong(messageCursor.getString(messageCursor.getColumnIndex(COLUMN_NAME_DATE)));
}
Complete Source code for this class is located at:
https://github.com/midhunhk/message-counter/blob/unicorn/messageCounter/src/main/java/com/ae/apps/messagecounter/observers/SMSObserver.java
I was able to solve this issue by using a different approach.
It seems like when we query the table SMSManager.SMS_URI_SENT as in the question, it is only returning 1 row which is the last message that was sent.
Cursor newMessagesCursor = context.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_ALL),
MessagesTableConstants.SMS_TABLE_PROJECTION,
"person is null and " + MessagesTableConstants.COLUMN_NAME_DATE + "> ? ",
new String[]{String.valueOf(lastMessageTimeStamp)}, null);
I query for SMSManager.SMS_URI_ALL and check for person column as null which represents messages that are sent.
I'm developing a call logs app. using below code, callType possible values I know are:
1 = incoming call answered, 2 = outgoing call, 3 = incoming call not answered, 5 = incoming call rejected.
what are values for outgoing call not answered" and "outgoing call rejected"
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Uri callUri = Uri.parse("content://call_log/calls");
Cursor cur = cr.query(callUri, null, null, null, strOrder);
// loop through cursor
while (cur.moveToNext()) {
String callType = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.TYPE));
}
You should never use an int to check for a type (eg. if (callType == 1)) as that 1 may mean something else in different Android versions. Instead, use the constants given by the API. In your case, these are
CallsLog.Calls.INCOMING_TYPE for incoming calls
CallsLog.Calls.MISSED_TYPE for missed calls
CallsLog.Calls.OUTGOING_TYPE for outgoing calls
CallsLog.Calls.VOICEMAIL_TYPE for voicemail calls
You should never bother which constant has what value (1, 2 or 3), as those may change, but their names (INCOMING_TYPE, MISSED_TYPE, OUTGOING_TYPE, VOICEMAIL_TYPE) definitely won't change.
Hope this helped!
i know from Phonestatelistener we will get to know, the call type like its incoming, or ringing or picked etc.. But once call ends i.e once phone reaches idle state, i want to know what was the state of call, was it picked, missed or rejected
lets take, +91123456789 is an incoming call, after call ends, the number will be stored in phone call log as missed call,
is there a way to fetch the recent state from call log for the particular number +91123456789, is it possible?
Here is code that can query the call log for a missed call. Basically, you will have to trigger this and make sure that you give the call log some time ( a few seconds should do it) to write the information otherwise if you check the call log too soon you will not find the most recent call.
int MISSED_CALL_TYPE = android.provider.CallLog.Calls.MISSED_TYPE
final String[] projection = null;
final String selection = null;
final String[] selectionArgs = null;
final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor cursor = null;
try{
// cursor = context.getContentResolver().query(
// Uri.parse("content://call_log/calls"),
// projection,
// selection,
// selectionArgs,
// sortOrder);
cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + "=? ", yourNumber, sortOrder);
while (cursor.moveToNext()) {
String callLogID = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls._ID));
String callNumber = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
String callDate = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DATE));
String callType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
String isCallNew = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NEW));
if(Integer.parseInt(callType) == MISSED_CALL_TYPE && Integer.parseInt(isCallNew) > 0){
if (_debug) Log.v("Missed Call Found: " + callNumber);
break ;
}
}
}catch(Exception ex){
if (_debug) Log.e("ERROR: " + ex.toString());
}finally{
cursor.close();
}
I hope you find this useful, in the same way you can get other states.
Do add this permission in manifest
android.permission.READ_CONTACTS
i would like to develop an app that send a message when ever that receives a missed
call.but i have a problem when ever i start that activity that send the sms to previous
ones how to solve this problem,i have a small idea a set that particular missed call as
read or delete it other wise my app has to access the missed the missed calls i a
specific peroid of time
try{
cursor = getContentResolver().query(
Uri.parse("content://call_log/calls"),
projection,
selection,
selectionArgs,
sortOrder);
// cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.MISSED_TYPE));
while (cursor.moveToNext()) {
String callLogID = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls._ID));
String callNumber = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
String callDate = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DATE));
String callType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
String isCallNew = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NEW));
//String missedcall= cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.MISSED_TYPE));
/*if(Integer.parseInt(callType) == android.provider.CallLog.Calls.MISSED_TYPE && Integer.parseInt(isCallNew) > 0){
tv.append("hai");
//if (_debug) Log.v("Missed Call Found: " + callNumber);
}*/
if(cursor.getString(cursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.TYPE)).equalsIgnoreCase("3") && Integer.parseInt(isCallNew)>0)
{
tv.append("ok"+callNumber+"\n");
sm.sendTextMessage(callNumber, null, " iam in work i will call you later", null, null);
}
}
}catch(Exception ex){
//if (_debug) Log.e("ERROR: " + ex.toString());
I guess you should make a broadcastreceiver that listenens to the phone state like this one:
broadcast receiver for missed call in android
I'm stuck with the call_log functionality in Froyo. As many of you know, Froyo logs in call log not only calls but also each outgoing and incomming SMS message. You can chose in options to show all that crap, or only specific types (outgoing calls, incoming calls, sent messages, received messages etc), but since this is radio button, you cannot specify for example only ongoing and incoming calls. Very known and annoing Froyo functionality.
So I started to write some simple tool to read the call log by myself. Here is the code snippet:
try {
mCur = getApplicationContext().getContentResolver()
.query(CallLog.Calls.CONTENT_URI, columns, null, null, null );
mCur.moveToFirst();
io = mCur.getColumnIndex(CallLog.Calls._ID);
bo = mCur.getColumnIndex(CallLog.Calls.NUMBER);
no = mCur.getColumnIndex(CallLog.Calls.CACHED_NAME);
to = mCur.getColumnIndex(CallLog.Calls.TYPE);
while (mCur.isAfterLast() == false) {
i = mCur.getString(io);
b = mCur.getString(bo);
n = mCur.getString(no);
t = mCur.getString(to);
Log.i(TAG, "CallLog: ID="+i+" number="+b+" name="+n+" type="+t);
mCur.moveToNext();
}
} catch (Exception e) {
Log.e(TAG, "updateCallLog", e);
} finally {
if (mCur != null) {
mCur.close();
mCur = null;
}
}
Surprise, surprise, the call_log provider skips the sms records from the call log. So with the code above I see only call records (incoming or outgoing), all other records are skipped. The little more digging into it revealed that the CallLog provider adds internally filtering to the call log database:
02-03 09:26:42.348 E/CLCService(28244): android.database.sqlite.SQLiteException:
near ")": syntax error: , while compiling:
SELECT _id, name, number, type FROM logs WHERE (logtype=100 OR logtype=500) AND (_ID=)
Do not look for the syntax error, it was created on purpose to force provider to dump the SQL query by calling query(CallLog.Calls.CONTENT_URI, columns, "_ID=", null, null )). The (_ID=) is what is provided in the query, the rest of (logtype=100 OR logtype=500) is apparently added by the call log provider itself.
So I have two questions:
Where I can find in the Android code how the provider is adding the logtype filter? I was looking into CallLog.java and CallLogProvider.java and cannot find it.
How can I read all records from the call log in Froyo? I cannot bypass the call log provider and use my own SQL helper for this until I will not root the phone, which is not an option. Is there any other way to do it?
I'm not certain just what is going wrong but reading the call log to get just incoming or outgoing calls is simple enough. The sample below adds restrictions to the query so that it only returns data for outgoing calls made after a certain date. The where string uses question marks to indicate where the values from the wargs array should be substituted in to form the sql query.
About where the extra WHERE clause occurs. Almost certainly in the calllog provider implementation. The providers commonly have a switch statement that uses the uri that you use to open the provider and then adds restrictions based on the uri. The calllog one seems to be in packages/providers/ContactsProvider.
public static int getMinutesUsedSince(Context context, Date date) {
Uri uri = CallLog.Calls.CONTENT_URI;
String columns[] = new String[] { CallLog.Calls.DURATION };
String where = CallLog.Calls.TYPE + "=? AND " + CallLog.Calls.DATE + ">?";
String wargs[] = new String[] {
String.valueOf(CallLog.Calls.OUTGOING_TYPE),
String.valueOf(date.getTime())
};
String sortOrder = "date DESC";
Cursor c = context.getContentResolver().query(uri, columns, where, wargs, sortOrder);
long sum = 0;
int durationIndex = c.getColumnIndex(CallLog.Calls.DURATION);
if (c.moveToFirst()) {
do {
/* for each individual call, round up to the nearest minute */
long duration = c.getLong(durationIndex);
long minutes = (long)Math.ceil(duration / 60.0);
sum += minutes;
} while (c.moveToNext());
}
c.close();
return (int)sum;
}