如何在android中的SDcard目录中存储已删除的联系人

时间:2014-05-30 20:37:40

标签: android android-activity linked-list

我想将Android设备的已删除联系人存储在设备SD卡的目录中。

这是我的代码:

package com.example.conetntobserver2;

import java.util.LinkedList;

import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.ContactsContract;
import android.text.StaticLayout;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
static int mastercount;

static LinkedList linkedList1 = new LinkedList();
static LinkedList linkedList2 = new LinkedList();
static LinkedList<String> masterList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mastercount = getContactsCount();
    masterList = linkedList1;

    Handler handler = new Handler(Looper.getMainLooper());
    ContentObserver observer = new Second(handler, MainActivity.this);
    // Second observer=new Second(MainActivity.this);
    getContentResolver().registerContentObserver(
            ContactsContract.Contacts.CONTENT_URI, true, observer);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public int getContactsCount() {

    Cursor cursor = null;

    cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);

    if (cursor != null) {
        getContactList(cursor);
        cursor.close();
        return linkedList1.size();
    } else {
        return 0;
    }
}

private LinkedList<String> getContactList(Cursor cursor) {
    // TODO Auto-generated method stub
    linkedList1.clear();
    // linkedList2.clear();
    while (cursor.moveToNext()) {

        String name = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        linkedList1.add(phoneNumber);
    }
    // Log.d("MasterList", masterList.toString());
    Log.d("LinkedList 1", linkedList1.toString());
    return linkedList1;

}

public LinkedList<String> getModifiedContacts() {

    Cursor cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    linkedList2.clear();
    while (cursor.moveToNext()) {

        String name = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        linkedList2.add(phoneNumber);
        // deleted=removeContact(linkedList1, linkedList2);
    }
    Log.d("MODIFIED List of Contacts", linkedList2.toString());
    return linkedList2;
}

/*
 * if (linkedList1.size() < linkedList2.size()) {
 * 
 * for (x = 0; x < linkedList1.size() - 1; x++) { for (y = 0; y <
 * linkedList2.size() - 1; y++) { if (linkedList1.get(y) ==
 * (linkedList2.get(y))) { break; } } }
 * 
 * }
 * 
 * if (linkedList1.size() > linkedList2.size()) {
 * 
 * for (x = 0; x < linkedList2.size() - 1; x++) { // String a =
 * linkedList2.get(i); for (y = 0; y < linkedList1.size() - 1; y++) { // if
 * (a == linkedList1.get(j)) { break; } }
 * 
 * }
 * 
 * }
 */
LinkedList<String> removeContact(LinkedList<String> one) {
    LinkedList<String> orignal = one;
    LinkedList<String> temp = masterList;
    Log.d("First List:- ", orignal.toString());
    Log.d("second List:- ", temp.toString());
    temp.removeAll(orignal);

    masterList = orignal;
    // masterList.addAll(one);
    mastercount = masterList.size();
    Log.d("A-B remove final List:- ", orignal.toString());
    // orignal.removeAll(masterList);
    Log.d("B-A remove final List:- ", temp.toString());
    return orignal;
}

LinkedList<String> updateContact(LinkedList<String> one) {
    LinkedList<String> orignal = one;
    LinkedList<String> two = getModifiedContacts();
    orignal.removeAll(two);
    return orignal;
}

LinkedList<String> addedContact(LinkedList<String> one,
        LinkedList<String> two) {
    LinkedList<String> orignal = one;
    orignal.removeAll(masterList);
    return orignal;
}
}

package com.example.conetntobserver2;

import java.util.LinkedList;

import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.provider.ContactsContract;
import android.util.Log;

public class Second extends ContentObserver {

MainActivity activity;
// static LinkedList linkedList;
static LinkedList list;
static int count = 0;

public Second(Handler handler, MainActivity activity2) {

    // TODO Auto-generated constructor stub
    super(handler);
    activity = activity2;

}

@Override
public void onChange(boolean selfChange) {
    super.onChange(selfChange);
    list = activity.getModifiedContacts();
    Log.d("hello", "" + list);
    final int currentCount = list.size();
    if (currentCount < activity.mastercount) {
        activity.removeContact(list);
    }
}
}

使用此代码,我在联系人列表上执行删除后会收到已删除的联系人(仅首次提取联系人)。但是,当我尝试从联系人列表中删除第二个联系人时,我无法获取已删除的联系人。

0 个答案:

没有答案