我想从通话记录,联系人列表和收件箱中检索电话号码,以便将该号码与列表进行比较,并在listview
中仅显示一次。
我能够从通话记录,联系人列表和收件箱中获取联系人,但问题是我无法比较通话记录和联系人列表中的号码,因为它们存储在不同的格式。
我的问题是,无论数字形成如何,我如何比较这两个数字?
目前的代码如下:
public class MainActivity extends Activity {
TextView textView = null;
ArrayList<String> call_log_no;
ArrayList<String> call_log_name;
String strStatus = "";
public boolean[] status;
StringBuffer sb;
String phNum,cname,details;
MatrixCursor mMatrixCursor;
ArrayList<String> choiceList;
ListView lvCountries;
HashMap<String,Object> hm;
int pos;
List<HashMap<String,Object>> aList = new ArrayList<HashMap<String,Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
/** Restore from the previous state if exists */
if(savedInstanceState!=null){
status = savedInstanceState.getBooleanArray("status");
}
call_log_no = new ArrayList<String>();
call_log_name = new ArrayList<String>();
choiceList = new ArrayList<String>();
lvCountries = (ListView) findViewById(R.id.lv_countries);
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> lv, View item, int position, long id) {
ListView lView = (ListView) lv;
SimpleAdapter adapter = (SimpleAdapter) lView.getAdapter();
HashMap<String,Object> hm = (HashMap) adapter.getItem(position);
/** The clicked Item in the ListView */
RelativeLayout rLayout = (RelativeLayout) item;
/** Getting the toggle button corresponding to the clicked item */
ToggleButton tgl = (ToggleButton) rLayout.getChildAt(0);
String strStatus = "";
if(tgl.isChecked()){
tgl.setChecked(false);
strStatus = "Off";
status[position]=false;
}else{
tgl.setChecked(true);
strStatus = "On";
status[position]=true;
}
Toast.makeText(getBaseContext(), (String) hm.get("no") + " : " + strStatus, Toast.LENGTH_SHORT).show();
}
};
lvCountries.setOnItemClickListener(itemClickListener);
// Each row in the list stores country name and its status
List<HashMap<String,Object>> aList = new ArrayList<HashMap<String,Object>>();
// The contacts from the contacts content provider is stored in this cursor
mMatrixCursor = new MatrixCursor(new String[] { "_id","name","details"} );
getCallDetails();
//Creating an AsyncTask object to retrieve and load listview with contacts
ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();
// Starting the AsyncTask process to retrieve and load listview with contacts
listViewContactsLoader.execute();
}
/** An AsyncTask class to retrieve and load listview with contacts */
private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor> {
@Override
protected Cursor doInBackground(Void... params) {
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
if(contactsCursor.moveToFirst()){
do{
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
Uri dataUri = ContactsContract.Data.CONTENT_URI;
// Querying the table ContactsContract.Data to retrieve individual items like
// home phone, mobile phone, work email etc corresponding to each contact
Cursor dataCursor = getContentResolver().query(dataUri, null,
ContactsContract.Data.CONTACT_ID + "=" + contactId,
null, null);
String displayName="";
String homePhone="";
String mobilePhone="";
String workPhone="";
if(dataCursor.moveToFirst()){
// Getting Display Name
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
do{
// Getting NickName
// Getting Phone numbers
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
boolean isStringExists = (call_log_no.contains(homePhone)||(call_log_no.contains("+91"+homePhone)));
if(isStringExists==true)
{
}
else{
call_log_no.add(homePhone);
call_log_name.add(displayName);
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
boolean isStringE = (call_log_no.contains(mobilePhone)||(call_log_no.contains("+91"+mobilePhone)));
if(isStringE==true)
{
}
else{
call_log_no.add(mobilePhone);
call_log_name.add(displayName);
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
boolean isStringExis = (call_log_no.contains(workPhone)||(call_log_no.contains("+91"+workPhone)));
if(isStringExis==true)
{
}
else{
call_log_no.add(workPhone);
call_log_name.add(displayName);
}
break;
}
}
}while(dataCursor.moveToNext());
String details = "";
// Adding id, display name, path to photo and other details to cursor
mMatrixCursor.addRow(new Object[]{ Long.toString(contactId),displayName,details});
}
}while(contactsCursor.moveToNext());
}
return mMatrixCursor;
}
@Override
protected void onPostExecute(Cursor result) {
status = new boolean[call_log_name.size()];
for(int i = 0,j = 0,k=0; i < call_log_name.size() ; i++,j++,k++)
{
HashMap<String, Object> hm = new HashMap<String,Object>();
status[k]=false;
// choiceList.add("\nName: "+call_log_name.get(i)+"\nPhone Number: " + call_log_no.get(j));
hm.put("no", call_log_no.get(i));
hm.put("txt", call_log_name.get(j));
hm.put("stat",status[k]);
aList.add(hm);
}
// Keys used in Hashmap
String[] from = {"txt","no","stat" };
// Ids of views in listview_layout
int[] to = {R.id.tv_item,R.id.tv_no, R.id.tgl_status};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.activity_main, from, to);
lvCountries.setAdapter(adapter);
}
}
private void getCallDetails() {
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
/* Query the CallLog Content Provider */
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
null, null, strOrder);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
int name= managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
while (managedCursor.moveToNext()) {
phNum = managedCursor.getString(number);
String callTypeCode = managedCursor.getString(type);
String strcallDate = managedCursor.getString(date);
Date callDate = new Date(Long.valueOf(strcallDate));
String callDuration = managedCursor.getString(duration);
cname=managedCursor.getString(name);
String callType = null;
int callcode = Integer.parseInt(callTypeCode);
switch (callcode) {
case CallLog.Calls.OUTGOING_TYPE:
callType = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callType = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callType = "Missed";
break;
}
boolean isStringExists = (call_log_no.contains(phNum)|| (call_log_no.contains("+91"+phNum)));
if(isStringExists==true)
{
}
else{
call_log_no.add(phNum);
call_log_name.add(cname);
} }
managedCursor.close();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBooleanArray("status", status);
}
}
请帮帮我......
答案 0 :(得分:0)
您可以使用以下库libphoneNumber
实现您的目标有一个名为isNumberMatch的方法,它考虑了国家代码。例如:
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumberUtil.MatchType match = phoneUtil.isNumberMatch(firstNumber, secondNumber);
if(match.name().equals("NSN_MATCH")){
//Its a match!
}