1

I'm trying to create an application that adds a missed call to the log when I was called why my phone was off. My provider sends me a text from that person saying e.g. Missed call: this person/number called at 23:46 on 28th Jan but left no message., so I've created a broadcast receiver to handle the receipt of the message. This part works fine. However I also want to be able to show the notification that there's been a missed call, and increment the number of missed calls. Is there any way of showing the notification?

Current code:

package uk.me.dtwood.missedcalls;

import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.CallLog;
import android.telephony.SmsMessage;
import android.util.Log;

public class MissedCallsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras(); 
        Object[] pdusObj = (Object[]) bundle.get("pdus"); 
        SmsMessage[] messages = new SmsMessage[pdusObj.length]; 
        for (int i = 0; i < pdusObj.length; i++) { 
            messages[i] = SmsMessage.createFromPdu ((byte[]) 
                    pdusObj[i]);  // AARGH! - see Javadoc for createFromPdu().
        }

        String testString = "Missed call: this person/number called at 23:46 on 28th Jan but left no message.";
        if (messages[0].getMessageBody().equals(testString))
        {
            abortBroadcast();
            ContentResolver cr = context.getContentResolver();
            Calendar callTime = Calendar.getInstance();
            insertPlaceholderCall(cr, messages[0].getOriginatingAddress(), callTime.getTimeInMillis());
        }
    }

    public static void insertPlaceholderCall(ContentResolver contentResolver, String number, Long addTime){
        ContentValues values = new ContentValues();
        values.put(CallLog.Calls.NUMBER, number);
        values.put(CallLog.Calls.DATE, addTime);
        values.put(CallLog.Calls.DURATION, 0);
        values.put(CallLog.Calls.TYPE, CallLog.Calls.MISSED_TYPE);
        values.put(CallLog.Calls.NEW, 1);
        values.put(CallLog.Calls.CACHED_NAME, "");
        values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
        values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
        Log.d("DEBUG", "Inserting call log placeholder for " + number + " at " + addTime);
        contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
    }
}
David Wood
  • 335
  • 1
  • 4
  • 9
  • Refer Link:http://developer.android.com/guide/topics/ui/notifiers/notifications.html – Arpit Garg Feb 04 '12 at 19:55
  • I would rather not create my own notification, as ideally I want it to also increment the number of missed calls on the lock screen, and also any things provided by other applications, for example my task list application will allow me to add calling that person back as a task. – David Wood Feb 04 '12 at 20:05
  • David well what you all desired can also be achieved by your own notification... Use the badge count for number thing and for the second thing lanuch an intent of call from the notification event – Arpit Garg Feb 04 '12 at 20:29
  • Have you tried just write to calllog with NEW set to 1 ?http://stackoverflow.com/questions/3166039/android-adding-number-to-call-logs – Eugene Nov 22 '13 at 18:15

0 Answers0