Search code examples
androidloggingservicecall

Call log detect changing


i have a question. I read a lot of tutorial that explain how to read call.log but my problem is how can i put call log reader into a service to detecting change? My application had to do some action when into call log there is a new outgoing call. Can someone help me? Regards


Solution

  • If you want to detect incoming call, you have to broadcast the action : ACTION_PHONE_STATE_CHANGED

    If you want to launch the broadcast in a service :

    public class ServDelLog extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d("SERVICE", "STARTED");
            //-- Here is the filter 
            IntentFilter filter = new IntentFilter();
            filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
            filter.setPriority(-1);
            registerReceiver(receiver, filter);
            //-- Inser your Code here ----
            ScanCallLog(this); //-- For exapmle a function for scaning the log
            //
            return START_STICKY;
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            unregisterReceiver(receiver);
        Log.d("SERVICE", "STOPPED");
        }
        @Override
        public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
        }
        private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, Intent intent) {
                String action = intent.getAction();
                if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
                //-- Because you've got to wait before the phone write into the call log
                    new CountDownTimer(5000, 1000) { 
                        public void onFinish() {
                            ScanCallLog(context);
                        }
                        public void onTick(long millisUntilFinished) {
                            // millisUntilFinished    The amount of time until finished.
                        }
                    }.start();
    
                }
            }
        };
    }
    

    The service can be set in your activity by calling :

    startService(new Intent(this, ServDelLog.class));
    

    And don't forget to add in your manifest :

    <service android:name=".ServDelLog" />