Hook some regular tasks?

断了今生、忘了曾经 提交于 2019-12-06 09:51:48
RivieraKid

Haven't tried any of this myself, but:

http://mylifewithandroid.blogspot.com/2008/03/observing-content.html seems to deal with detecting contact data changes. Basically you need to register a ContentObserver and handle the changes you are notified of.

Check out http://developer.android.com/reference/android/content/Intent.html - from that you can register a BroadcastReceiver to be notified of applications being installed or uninstalled. Look for ACTION_PACKAGE_ADDED and ACTION_PACKAGE_REMOVED

Please refer to How to detect file or folder changes in Android? for how to detect when files are changed in the filesystem. You may be limited to your sandbox with a FileObserver, I'm not sure. Also - a rename doesn't seem to be explicitly notified, but you will probably detect it from a MOVED_FROM followed by MOVED_TO, or possibly a DELETE followed by CREATE

Found in the SDK sample for SDK version 5+:

     /**
     * Retrieves the contact information.
     */
    @Override
    public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {
        ContactInfo contactInfo = new ContactInfo();
        long contactId = -1;

        // Load the display name for the specified person
        Cursor cursor = contentResolver.query(contactUri,
                new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null);
        try {
            if (cursor.moveToFirst()) {
                contactId = cursor.getLong(0);
                contactInfo.setDisplayName(cursor.getString(1));
            }
        } finally {
            cursor.close();
        }

        // Load the phone number (if any).
        cursor = contentResolver.query(Phone.CONTENT_URI,
                new String[]{Phone.NUMBER},
                Phone.CONTACT_ID + "=" + contactId, null, Phone.IS_SUPER_PRIMARY + " DESC");
        try {
            if (cursor.moveToFirst()) {
                contactInfo.setPhoneNumber(cursor.getString(0));
            }
        } finally {
            cursor.close();
        }

        return contactInfo;
    }

You can specify the contact columns you want to retreive with Cursor cursor = contentResolver.query(contactUri, new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null); The column names are described at http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html and looking at the sample, it seems cursor.getLong(0) here is the contact ID you're looking for. It also seems that it is volatile depending on how the contact is edited and how others are added, but you're catching those too so you should be able to handle those cases.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!