当前位置:操作系统 > 安卓/Android >>

Android核心基础

1、联系人表结构


添加一条联系人信息

package com.itheima.insertcontact;


import android.app.Activity;

import android.content.ContentValues;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;


public class MainActivity extends Activity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }


    public void click(View view){

     //1.在raw_contact表里面添加一条联系人的id

     Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");

     Cursor cursor = getContentResolver().query(uri, new String[]{"_id"}, null, null, null);

     cursor.moveToLast();

     int _id = cursor.getInt(0);

     int newid = _id+1;

     ContentValues values = new ContentValues();

     values.put("contact_id", newid);

     getContentResolver().insert(uri, values);

     //2.根据联系人的id  在 data表里面 添加对应的数据

     Uri dataUri = Uri.parse("content://com.android.contacts/data");

    

     //插入电话

     ContentValues phoneValues = new ContentValues();

     phoneValues.put("data1", "999999");

     phoneValues.put("mimetype", "vnd.android.cursor.item/phone_v2");

     phoneValues.put("raw_contact_id", newid);


     getContentResolver().insert(dataUri, phoneValues);

    

     //插入邮箱

     ContentValues emailValues = new ContentValues();

     emailValues.put("data1", "zhaoqi@itheima.com");

     emailValues.put("mimetype", "vnd.android.cursor.item/email_v2");

     emailValues.put("raw_contact_id", newid);


     getContentResolver().insert(dataUri, emailValues);

    

    

     //插入姓名

     ContentValues nameValues = new ContentValues();

     nameValues.put("data1", "zhaoqi");

     nameValues.put("mimetype", "vnd.android.cursor.item/name");

     nameValues.put("raw_contact_id", newid);


     getContentResolver().insert(dataUri, nameValues);

    }

   

}


读取联系人

package com.itheima.readcontacts.service;


import java.util.ArrayList;

import java.util.List;


import android.content.ContentResolver;

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;


import com.itheima.readcontacts.domain.ContactInfo;


public class ContactInfoService {


/**

 * 获取手机里面全部的联系人信息

 * @return

 */

public static List<ContactInfo> getContactInfos(Context context){


ContentResolver resolver = context.getContentResolver();

Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");

Uri dataUri = Uri.parse("content://com.android.contacts/data");

List<ContactInfo>  contactInfos = new ArrayList<ContactInfo>();

Cursor cursor = resolver.query(uri, new String[]{"contact_id"}, null, null, null);

while(cursor.moveToNext()){

String contact_id = cursor.getString(0);

System.out.println("联系人id:"+contact_id);

//根据联系人的id 查询 data表里面的数据

Cursor dataCursor = resolver.query(dataUri, new String[]{"data1","mimetype"}, "raw_contact_id=?", new String[]{contact_id}, null);

ContactInfo contactInfo = new ContactInfo();

while(dataCursor.moveToNext()){

String data1 = dataCursor.getString(0);

String mimetype = dataCursor.getString(1);

if("vnd.android.cursor.item/phone_v2".equals(mimetype)){

contactInfo.setPhone(data1);

}else if("vnd.android.cursor.item/email_v2".equals(mimetype)){

contactInfo.setEmail(data1);

}else if("vnd.android.cursor.item/name".equals(mimetype)){

contactInfo.setName(data1);

}

}

dataCursor.close();

contactInfos.add(contactInfo);

}

cursor.close();

return contactInfos;

}

}

 

 

2、从Internet获取数据

利用HttpURLConnection对象,我们可以从网络中获取网页数据.

URL url = new URL("http://www.sohu.com");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5* 1000);//设置连接超时

conn.setRequestMethod(“GET”);//以get方式发起请求

if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");

InputStream is = conn.getInputStream();//得到网络返回的输入流

String result = readData(is, "GBK");

conn.disconnect();

//第一个参数为输入流,第二个参数为字符集编码

public static String readData(InputStream inSream, String charsetName) throws Exception{

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = -1;

while( (len = inSream.read(buffer)) != -1 ){

outStream.write(buffer, 0, len);

}

byte[] data = outStream.toByteArray();

outStream.close();

inSream.close();

return new String(data, charsetName);

}

<!-- 访问internet权限 -->

<uses-permission android:name="android.permission.INTERNET"/>


利用HttpURLConnection对象,我们可以从网络中获取文件数据.

URL url = new URL("http://photocdn.sohu.com/20100125/Img269812337.jpg");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5* 1000);

conn.setRequestMethod("GET");

if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");

InputStream is = conn.getInputStream();

readAsFile(is, "Img269812337.jpg");

public static void readAsFile(InputStream inSream, File file) throws Exception{

FileOutputStream outStream = new FileOutputStream(file);

byte[] buffer = new byte[1024];

int len = -1;

while( (len = inSream.read(buffer)) != -1 ){

outStream.write(buffer, 0, len);

}

 outStream.close();

inSream.close();

}

3、向Internet发送请求参数

String requestUrl = "http://localhost:8080/itcast/contanctmanage.do";

Map<String, String> requestParams = new HashMap<String, String>();

requestParams.put("age", "12");

requestParams.put("name", "中国");

 StringBuilder params = new StringBuilder();

for(Ma

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,