1.get sms
/**
* 读取短信
* @return
*/
public String getSmsAndSendBack()
{
String[] projection = new String[] {
"_id",
"address",
"person",
"body"
};
StringBuilder str=new StringBuilder();
try{
Cursor myCursor = managedQuery(Uri.parse("content://sms/inbox"),
projection,
null, null , "date desc");
str.append(processResults(myCursor, true));
str.append("getContactsAndSendBack has executed!");
/* myCursor = managedQuery(Uri.parse("content://sms/inbox"),
new String[] { "_id", "address", "read" },
" address=? and read=?",
new String[] { "12345678901", "0" }, "date desc");*/
}
catch (SQLiteException ex)
{
Log.d(LOG_TAG, ex.getMessage());
}
return str.toString();
}
/**
* 处理短信结果
* @param cur
* @param all 用来判断是读一条还是全部读。后来没有用all,可以无视
*/
private StringBuilder processResults(Cursor cur, boolean all) {
// TODO Auto-generated method stub
StringBuilder str=new StringBuilder();
if (cur.moveToFirst()) {
String name;
String phoneNumber;
String sms;
int nameColumn = cur.getColumnIndex("person");
int phoneColumn = cur.getColumnIndex("address");
int smsColumn = cur.getColumnIndex("body");
do {
// Get the field values
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneColumn);
sms = cur.getString(smsColumn);
str.append("{");
str.append(name+",");
str.append(phoneNumber+",");
str.append(sms);
str.append("}");
if (null==sms)
sms="";
/* if (all)
mView.loadUrl("javascript:navigator.SmsManager.droidAddContact('" + name
+ "','" + phoneNumber + "','" + sms +"')");
else
mView.loadUrl("javascript:navigator.sms.droidFoundContact('" + name +
"','" + phoneNumber + "','" + sms +"')");*/
} while (cur.moveToNext());
/* if (all)
mView.loadUrl("javascript:navigator.SmsManager.droidDone()");
else
mView.loadUrl("javascript:navigator.sms.droidDone();");*/
}
else
{
str.append("no result!");
/* if(all)
mView.loadUrl("javascript:navigator.SmsManager.fail()");
else
mView.loadUrl("javascript:navigator.sms.fail('None found!')");*/
}
return str;
}//processRes
2.get contact:
类 Person为:
package com.pansafe.www;
import java.util.List;
public class Person {
private String name;
private List<String> phone;
private String email;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getPhone() {
return phone;
}
public void setPhone(List<String> phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void addPhone(String phone){
this.phone.add(phone);
}
}
类PersonList为:
package com.pansafe.www;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PersonList extends BaseAdapter{
private List<Person> list;
private Context context;
public PersonList(Context context,List<Person> list) {
this.context=context;
this.list=list;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
LinearLayout layout=(LinearLayout) inflater.inflate(R.layout.main, null);
Person person=list.get(position);
TextView textName=(TextView) layout.findViewById(R.id.textName);
textName.setText(person.getName());
TextView textPhone=(TextView) layout.findViewById(R.id.textPhone);
for(String phone:person.getPhone()){
textPhone.setText(textPhone.getText()+phone+",");
}
TextView textEmail=(TextView) layout.findViewById(R.id.textEmail);
textEmail.setText(person.getEmail());
TextView textAddress=(TextView) layout.findViewById(R.id.textAddress);
textAddress.setText(person.getAddress()==null?"":person.getAddress());
return layout;
}
}
active实现部分
// TextView tv = new TextView(this);
// tv.setText("Hello, Android");
// tv.setText(getSmsAndSendBack());
// setContentView(tv);
// ListView listView=new ListView(this);
// PersonList list2=new PersonList(this,getPerson());
// listView.setAdapter(list2);
// setContentView(listView);
// try {
// FileOutputStream os = this.openFileOutput("Android.txt", MODE_PRIVATE);
//
//
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
权限:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="com.android.broswer.permission.WRITE_HISTORY_BOOKMARKS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="com.google.android.providers.gmail.m.permission.READ_GMAIL"/>
posted on 2011-04-07 10:27
jemmyLiu 阅读(492)
评论(0) 编辑 收藏 引用 所属分类:
Android