당신은이 코드를 사용해야하고 그것은 100 % 작품이다.
(1) 당신은 서비스
startService(new Intent(this, CallServices.class));
(2) CallServices 클래스를 확인하고 contentobser을 시작해야
을 시작해야합니다.
public class CallServices extends Service{
private static final String TAG = "CallService";
Context mContext;
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
return null;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
mContext=getApplicationContext();
}
@Override
public void onStart(Intent intent, int startId) {
//super.onStart(intent, startId);
Log.d(TAG, "onStart services is started");
Uri mediaUri = Uri.parse("content://call_log/calls");
Handler handler = new Handler(){};
CustomContentObserver custObser = new CustomContentObserver(handler,mContext);
mContext.getContentResolver().registerContentObserver(mediaUri, true, custObser);
}
}
(3) 통화 기록 할 얻기에 CustomContentObserver 클래스를 확인합니다.
public class CustomContentObserver extends ContentObserver {
long lastTimeofCall = 0L;
long lastTimeofUpdate = 0L;
long threshold_time = 10000;
public CustomContentObserver(Handler handler,Context con) {
super(handler);
this.mContext=con;
System.out.println("Content obser");
callflag=true;
}
@Override
public boolean deliverSelfNotifications() {
return false;
}
public void onChange(boolean selfChange) {
super.onChange(selfChange);
lastTimeofCall = System.currentTimeMillis();
if(lastTimeofCall - lastTimeofUpdate > threshold_time){
if(callflag){
System.out.println("Content obser onChange()");
Log.d("PhoneService", "custom StringsContentObserver.onChange(" + selfChange + ")");
//if(!callFlag){
String[] projection = new String[]{CallLog.Calls.NUMBER,
CallLog.Calls.TYPE,
CallLog.Calls.DURATION,
CallLog.Calls.CACHED_NAME,
CallLog.Calls._ID};
Cursor c;
c=mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls._ID + " DESC");
if(c.getCount()!=0){
c.moveToFirst();
lastCallnumber = c.getString(0);
lastCallnumber=FormatNumber(lastCallnumber);
String type=c.getString(1);
String duration=c.getString(2);
String name=c.getString(3);
String id=c.getString(4);
System.out.println("CALLLLing:"+lastCallnumber+"Type:"+type);
Database db=new Database(mContext);
Cursor cur =db.getFirstRecord(lastCallnumber);
System.out.println("CUSTOM GALLARY..."+cur.getCount());
final String endCall=lastCallnumber;
//checking incoming/outgoing call
if(type.equals("1")){
//incoming call code
}else if(type.equals("2")){
//outgoing call code
} else{
//missed call code
}
}
lastTimeofUpdate = System.currentTimeMillis();
}
}
}
} 당신이 통화 기록 데이터를 읽을 수있는이 방법으로
. 콘텐츠를 사용하고 있습니다. 왜냐하면 htc 장치에서 phonestateListner를 읽을 수 없기 때문입니다.
서비스는 무엇입니까? BroadcastReceiver? –
선택 사항입니다. 활동을 현장에서 직접 사용할 수 있습니다. –
어떻게 그것을 필드로 사용할 수 있습니까? –