呼叫日志检测到更改



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

如果您想检测来电,您必须广播操作: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" />

最新更新