您的位置:首页 > 其它

Content Provider

2015-12-24 14:59 441 查看
1.概念

主要用于不同应用程序之间实现数据共享的功能,允许一个程序访问另一个程序中的数据,并保证被访数据的安全性。一般情况下是使用现有的内容提供器来读取和操作相应程序中的数据。

2.基本用法(自己写的打电话小程序)

<TableLayout 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"
android:stretchColumns="1"
tools:context="${relativePackage}.${activityClass}" >

<TableRow>

<TextView
android:layout_height="wrap_content"
android:text="电话号码:" />

<EditText
android:id="@+id/et_PhoneNumber"
android:layout_height="wrap_content"
android:hint="请输入电话号码"
android:inputType="phone" />

<Button
android:id="@+id/btn_ChooseContact"
android:layout_height="wrap_content"
android:text="选择联系人" />
</TableRow>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短信内容:" />

<EditText
android:id="@+id/et_SMSContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入短信内容" />
</LinearLayout>

<RelativeLayout>

<Button
android:id="@+id/btn_CallPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打电话" />

<Button
android:id="@+id/btn_SendSMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="发送短信" />
</RelativeLayout>

</TableLayout>


/MyPhone/res/layout/contact_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_personName"
android:layout_width="150dp"
android:layout_height="50dp"
android:paddingLeft="30dp"
android:text="姓名"
android:textSize="18sp"
android:gravity="center_vertical" />

<TextView
android:id="@+id/tv_phoneNumber"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:text="电话号码"
android:textSize="18sp" />

</LinearLayout>


/MyPhone/res/layout/contact_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/lv_ContactList"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>


/MyPhone/src/com/myphone/app/MainActivity.java

package com.myphone.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

private EditText et_PhoneNumber, et_SMSContent;
private Button btn_CallPhone, btn_SendSMS, btn_ChooseContact;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_PhoneNumber = (EditText) findViewById(R.id.et_PhoneNumber);
et_SMSContent = (EditText) findViewById(R.id.et_SMSContent);
btn_CallPhone = (Button) findViewById(R.id.btn_CallPhone);
btn_SendSMS = (Button) findViewById(R.id.btn_SendSMS);
btn_ChooseContact = (Button) findViewById(R.id.btn_ChooseContact);

btn_ChooseContact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ContactList.class);
startActivityForResult(intent, 1);
}
});

btn_CallPhone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("提示:")
.setCancelable(false)
.setMessage("确认拨打电话?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + et_PhoneNumber.getText()));
startActivity(intent);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
});

btn_SendSMS.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("提示:").setCancelable(true)
.setMessage("是否发送短信?")
.setPositiveButton("是的", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 创建一个包含电话号的uri数据
Uri data = Uri.parse("smsto:" + et_PhoneNumber.getText());
// 指向短信编辑待发送界面的intent
Intent intent = new Intent(Intent.ACTION_SENDTO, data);
// 携带短信内容数据
intent.putExtra("sms_body", et_SMSContent.getText().toString());
startActivity(intent);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bundle bundle;
switch (resultCode) {
case 1:
bundle = data.getExtras();
String number = bundle.getString("number");
et_PhoneNumber.setText(number);
break;
default:
break;
}
}
}


/MyPhone/src/com/myphone/app/Contact.java

package com.myphone.app;

public class Contact {
private String personName;
private String phoneNumber;

public String getPhoneNumber() {
return phoneNumber;
}

public String getPersonName() {
return personName;
}

public Contact(String personName, String phoneNumber) {
this.personName = personName;
this.phoneNumber = phoneNumber;
}
}


/MyPhone/src/com/myphone/app/ContactAdapter.java

package com.myphone.app;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ContactAdapter extends ArrayAdapter<Contact> {

private int resourceId;

public ContactAdapter(Context context, int textViewResourceId, List<Contact> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Contact contact = getItem(position);// 获取当前项的Contact实例
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}
TextView personName = (TextView) view.findViewById(R.id.tv_personName);
TextView phoneNumber = (TextView) view.findViewById(R.id.tv_phoneNumber);
personName.setText(contact.getPersonName());
phoneNumber.setText(contact.getPhoneNumber());
return view;
}
}


/MyPhone/src/com/myphone/app/ContactList.java

package com.myphone.app;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class ContactList extends Activity {

private List<Contact> contactList = new ArrayList<Contact>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);

ContactAdapter adapter = new ContactAdapter(ContactList.this, R.layout.contact_item, contactList);

ListView contactListView = (ListView) findViewById(R.id.lv_ContactList);

contactListView.setAdapter(adapter);
readContactList();

contactListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Contact contact = contactList.get(position);
Toast.makeText(ContactList.this, contact.getPersonName() + "\n" + contact.getPhoneNumber(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("number", contact.getPhoneNumber());
intent.putExtras(bundle);
setResult(1, intent);
finish();
}
});
}

// 读取联系人的方法
private void readContactList() {
Cursor cursor = null;
try {
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,
null);
while (cursor.moveToNext()) {
// 获取联系人姓名
String displayName = cursor
.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// 获取联系人手机号
String phoneNumber = cursor
.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact contact = new Contact(displayName, phoneNumber);
contactList.add(contact);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: