Android 애플 릿 액세스 연락처 구현
요청:
프로그램 을 작성 하고 ContentProvider 를 사용 하여 연락처 에 접근 합 니 다.
ContentProvider 클래스 의 역할:
ContentProvider(콘 텐 츠 제공 기)는 모든 응용 프로그램 간 의 데이터 저장 과 검색 의 교량 으로 각 응용 프로그램 간 에 데 이 터 를 공유 할 수 있 는 역할 을 한다.주요 기능 은 데 이 터 를 저장 하고 검색 하 며 응용 프로그램 에 접근 데 이 터 를 제공 하 는 인터페이스 이다.
기본 동작:
조회:ContentResolver 의 query()방법 으로 데 이 터 를 조회 하 는 것 은 SQLite 조회 와 마찬가지 로 결과 집합 을 가리 키 는 커서 Cursor 를 되 돌려 줍 니 다.
삽입:ContentResolver.insert()방법 을 사용 하여 ContentProvide 에 새 기록 을 추가 할 때,먼저 기록 한 데 이 터 를 ContentValues 대상 에 봉 한 다음,ContentResolver.insert()방법 을 호출 하면 URI 를 되 돌려 줍 니 다.이 URI 내용 은 ContentProvider 의 URI 에 새 기록 의 확장 ID 를 더 해서 얻 은 것 입 니 다.이 URI 를 통 해 이 기록 에 대해 진일보 한 조작 을 할 수 있 습 니 다.
삭제:단일 기록 을 삭제 하려 면 ContentResolver.delete()방법 을 사용 하여 특정 줄 의 URI 인 자 를 전달 하여 삭제 작업 을 수행 할 수 있 습 니 다.여러 줄 의 기록 에 대해 삭제 작업 을 수행 하려 면 삭 제 될 기록 형식의 URI 와 where 자 구 를 delete()방법 으로 전달 하여 여러 줄 의 삭 제 를 실현 해 야 합 니 다.
업데이트:ContentResolver.update()방법 으로 기록 업데이트 작업 을 수행 합 니 다.
구현 방안:
(1)CPActivity.java 프로그램 코드 는 다음 과 같 습 니 다.
package com.example.contentprovider;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.widget.TextView;
public class CPActivity extends Activity {
Uri contact_uri = Contacts.CONTENT_URI;// URI
// TextView
TextView textview;
//
int textcolor = Color.BLACK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// main.xml UI
setContentView(R.layout.activity_cp);
textview = (TextView)findViewById(R.id.textview);
// getContactInfo()
String result = getContactInfo();
//
textview.setTextColor(textcolor);
//
textview.setTextSize(20.0f);
//
textview.setText(" \t
"+result);
}
//getContactInfo() , String
public String getContactInfo() {
// TODO Auto-generated method stub
String result = "";
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(contact_uri, null, null, null, null);
// _ID
int idIndex = cursor.getColumnIndex(Contacts._ID);
// name
int nameIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
// Cursor
cursor.moveToFirst();
for(;!cursor.isAfterLast();cursor.moveToNext()){
result = result+cursor.getString(idIndex)+"\t\t\t";
result = result+cursor.getString(nameIndex)+"\t
";
}
// close
cursor.close();
//
return result;
}
}
(2)Activity_cp.xml 코드 는 다음 과 같 습 니 다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
(3)그 다음 에 AndroidManifest.xml 에 다음 과 같은 권한 을 추가 해 야 합 니 다.
<uses-permission
android:name="android.permission.READ_CONTACTS" />
(4)실현 효과:연락처 에 몇 개의 연락 처 를 추가 합 니 다.
프로그램 을 실행 하면 핸드폰 에 있 는 모든 연락처 의 ID 와 이름 이 기 록 됩 니 다.
프로그램 을 실행 하면 핸드폰 에 있 는 모든 연락처 의 ID 와 이름 이 기 록 됩 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.