呼叫日志检测更改

时间:2015-11-06 22:04:52

标签: android logging service call

我有一个问题。 我阅读了很多解释如何阅读call.log的教程但我的问题是如何将调用日志阅读器放入服务中来检测更改? 我的应用程序必须在进入呼叫日志时执行一些操作,有一个新的拨出呼叫。有人能帮我吗? 此致

1 个答案:

答案 0 :(得分:0)

如果您想检测来电,则必须广播该操作: ACTION_PHONE_STATE_CHANGED

如果您想在服务中启动广播:

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();

            }
        }
    };
}

可以通过调用以下内容在您的活动中设置服务:

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

不要忘记添加你的清单:

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