Android 애플 릿 액세스 연락처 구현

4290 단어 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 와 이름 이 기 록 됩 니 다.

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기