答案 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" />