2016-08-02 101 views
2

只要通话记录发生变化,我就能得到通知,但我也需要号码我想知道通话的电话号码,从通话记录中删除

这里是我的代码:

public class MainActivity extends AppCompatActivity {  
    CallLogChangeObserverClass callLogChangeObserverClass = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     callLogChangeObserverClass = new CallLogChangeObserverClass(new Handler(), this); 

     //Registering content observer 
     getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, 
       callLogChangeObserverClass); 

    } 


    public class CallLogChangeObserverClass extends ContentObserver { 
     public CallLogChangeObserverClass(Handler handler, Context ct) { 
      super(handler); 
      // TODO Auto-generated constructor stub 
     } 

     public void onChange(boolean selfChange) { 
      Toast.makeText(getApplicationContext(), "Call Log Changed", Toast.LENGTH_LONG).show(); 
      //Read call Logs here 
     } 
    } 

回答

2

最后我得到了解决此,但,但我不知道是否方式正确与否。这个工程通话记录和ContactNumber变化

中的onChange

execute a Query with contentResolver(), and put the data in a List 
then something is deleted again 
execute a Query with contentResolver(),and assign it to tempList. 
now compare the List and tempList 



@Override 
     public void onChange(boolean selfChange) { 
      super.onChange(selfChange);  
final int List = totalContacts(); 

if (tempList < List) { 
something is deleted 
then remove the tempList from List you will get the deleted number 

}else if (tempList == List) { 
something is updated 
then remove the tempList from List you will get the deleted number 
}else { 
     something is added(reverse the Lists) 
then remove the List from tempList you will get the deleted number 
    } 
    }