我想知道以下是否可行:
我的应用(服务)保留在后台,并在用户
时被触发我试图稍微查看一下互联网,但没有找到与此相关的讨论。
你的猜测是什么?
答案 0 :(得分:3)
我自己没有尝试过这些,但是:
http://mylifewithandroid.blogspot.com/2008/03/observing-content.html似乎涉及检测联系人数据的变化。基本上,您需要注册ContentObserver
并处理您收到通知的更改。
签出http://developer.android.com/reference/android/content/Intent.html - 您可以注册一个BroadcastReceiver,以获得有关正在安装或卸载的应用程序的通知。查找ACTION_PACKAGE_ADDED
和ACTION_PACKAGE_REMOVED
请参阅How to detect file or folder changes in Android?了解如何检测文件系统中文件的更改时间。您可能仅限于使用FileObserver的沙箱,我不确定。此外 - 似乎没有明确通知重命名,但您可能会从MOVED_FROM
后跟MOVED_TO
或可能DELETE
后跟CREATE
<检测到它/ p>
答案 1 :(得分:1)
在SDK版本5 +的SDK示例中找到:
/**
* 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;
}
您可以使用Cursor cursor = contentResolver.query(contactUri, new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null);
指定要进行检索的联系人列。http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html中描述了列名称并查看示例,此处cursor.getLong(0)
似乎是您的联系人ID正在寻找。它似乎也是不稳定的,这取决于联系人的编辑方式以及其他人的添加方式,但是你也会抓住这些,所以你应该能够处理这些情况。