Android용 Gmail 앱에는 서드 파티 개발자가 이름, 읽지 않은 수와 같은 라벨 정보를 가져오고 정보가 변경될 때 업데이트된 상태를 유지하는 데 사용할 수 있는 콘텐츠 제공자 가 포함되어 있습니다. 예를 들어 앱 또는 위젯은 특정 계정의 받은편지함에 있는 읽지 않은 수를 표시할 수 있습니다.
이 콘텐츠 제공자를 사용하기 전에
GmailContract.canReadLabels(Context)
메서드를 호출하여 사용자의 Gmail 앱 버전이
이러한 쿼리를 지원하는지 확인하세요.
쿼리할 유효한 Gmail 계정 찾기
앱은 먼저 라벨 정보를 쿼리할 유효한 Gmail 계정의 이메일 주소를 찾아야 합니다.
GET_ACCOUNTS
권한이 있으면
AccountManager
가 이 정보를 반환할 수 있습니다.
// Get the account list, and pick the first one.
final String ACCOUNT_TYPE_GOOGLE = "com.google";
final String[] FEATURES_MAIL = {
"service_mail"
};
AccountManager.get(this).getAccountsByTypeAndFeatures(ACCOUNT_TYPE_GOOGLE, FEATURES_MAIL,
new AccountManagerCallback() {
@Override
public void run(AccountManagerFuture future) {
Account[] accounts = null;
try {
accounts = future.getResult();
if (accounts != null && accounts.length > 0) {
String selectedAccount = accounts[0].name;
queryLabels(selectedAccount);
}
} catch (OperationCanceledException oce) {
// TODO: handle exception
} catch (IOException ioe) {
// TODO: handle exception
} catch (AuthenticatorException ae) {
// TODO: handle exception
}
}
}, null /* handler */);
콘텐츠 제공자 쿼리
이메일 주소를 선택하면 쿼리할 수 있는
ContentProvider
URI를 가져올 수 있습니다. URI를 구성하고 반환된 열을 정의하는
GmailContract
라는 클래스를 제공했습니다. 앱은 이 URI를 직접 쿼리할 수 있습니다.
Cursor의 데이터를 사용하면
GmailContract.Labels.URI
열에 URI 값을 유지하여 단일 라벨의 변경사항을 쿼리하고 감시할 수 있습니다.
미리 정의된 라벨의 NAME 값은 언어에 따라 다를 수 있으므로
GmailContract.Labels.NAME을 사용하지 마세요.
대신
GmailContract.Labels.CANONICAL_NAME
`GmailContract.Labels.CANONICAL_NAME` 열의 문자열 값을 사용하여 받은편지함,
보낸편지함 또는 임시보관함과 같은 미리 정의된 라벨을 프로그래매틱 방식으로 식별할 수 있습니다.
// Query for all labels and find the Inbox.
try (Cursor labelsCursor = getContentResolver().query(
GmailContract.Labels.getLabelsUri(selectedAccount), null, null, null, null)) {
if (labelsCursor != null) {
final String inboxCanonicalName = GmailContract.Labels.LabelCanonicalName.CANONICAL_NAME_INBOX;
final int canonicalNameIndex = labelsCursor.getColumnIndexOrThrow(GmailContract.Labels.CANONICAL_NAME);
while (labelsCursor.moveToNext()) {
if (inboxCanonicalName.equals(labelsCursor.getString(canonicalNameIndex))) {
// This row corresponds to the Inbox.
}
}
}
}
자세한 내용은 콘텐츠 제공자 기본사항을 참고하세요.
예시 검토
이 콘텐츠 제공자가 실제로 작동하는 예시를 보려면 샘플 앱을 다운로드하세요.