<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWorldActivity!</string>
<string name="app_name">电话拨号器</string>
<string name="mobile">请输入手机号</string>
<string name="button">拨打</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mobile"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
package="cn.andriod"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloWorldActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>
package cn.andriod;
import android.app.Activity;
t.Intent;
import android.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class PhoneActivity extends Activity {
private EditText mobileTest;
private Button mobileButton;
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
mobileTest = (EditText) this.findViewById(bile);
mobileButton = (Button) this.findViewById(R.id.button);
mobileButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String mobile = Text().toString();
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+mobile));
PhoneActivity.this.startActivity(intent); //内部类,访问外部类方法
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">发送短信</string>
<string name="mobile">请输入手机号</string>
<string name="content">请输入短信内容</string>
<string name="button">发送短信</string>
<string name="info">发送成功!</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mobile"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:id="@+id/content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
android;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
lephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SMSendActivty extends Activity {
private EditText moblieText;
private EditText contextText;
@Override
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
moblieText = (EditText) this.findViewById(bile);
contextText = (EditText) this.findViewById(t);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String mobile = Text().toString();
String content = Text().toString();
SmsManager smsManager = Default();
if(content.length() > 70){
List<String> contexts = smsManager.divideMessage(content);
for(String sms : contexts){
smsManager.sendTextMessage(mobile, null, sms, null, null);
}
}else{
smsManager.sendTextMessage(mobile, null, content, null, null);
}//Ctrl+T
contextText.setText("");
Toast.makeText(SMSendActivty.this, R.string.info, Toast.LENGTH_LONG).show();
}
});
}
}
package cn.log;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class LogActivity extends Activity {
private static final String TAG = "LogActivity"; //shift+ctrl+x
@Override
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "LogActivity 被创建了!"); //info级别
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
package="cn.log"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name=st.runner" />
<activity android:name=".LogActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<instrumentation android:name=st.InstrumentationTestRunner"
android:targetPackage="cn.log" android:label="Tests for My App"/>
</manifest>
package cn.log;
st.AndroidTestCase;
import android.util.Log;
public class LogTest extends AndroidTestCase {
private static final String TAG = "LogTest";
public void testSave() throws Exception{
int i = 1 + 1;
Log.i(TAG,"结果为"+i);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
package="cn.file"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name=st.runner" />
<activity android:name=".FileActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<instrumentation android:name=st.InstrumentationTestRunner"
android:targetPackage="cn.file" android:label="Tests for My App"/>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">文件管理</string>
<string name="FileName">文件名称:</string>
<string name="content">文件内容</string>
<string name="save">保存</string>
<string name="error">保存失败</string>
<string name="sucess">保存成功</string>
<string name="show">显示内容</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout xmlns:android=""
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/FileName"
android:id="@+id/filelable"
/>
<EditText
android:layout_width="150px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/filelable"
android:layout_alignTop="@id/filelable"
android:layout_marginLeft="10px"
android:text="
android:id="@+id/filename"
/>
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:id="@+id/content"
/>
<RelativeLayout xmlns:android=""
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/save"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/save"
android:layout_alignTop="@id/save"
android:layout_marginLeft="10px"
android:text="@string/show"
android:id="@+id/show"
/>
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/result"
/>
</LinearLayout>
package cn.service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileService {
/**
* 保存数据
* @param outputStream
* @param content
* @throws IOException
*/
public static void save(OutputStream outputStream,String content) throws IOException{
outputStream.Bytes());
outputStream.close();
}
/**
* 读取数据
* @param inputStream
* @return
* @throws IOException
*/
public static String read(InputStream inputStream) throws IOException{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
int len = -1;
while((len ad(buffer)) !=-1){
outputStream.write(buffer,0,len);
}
byte[] data = ByteArray();
outputStream.close();
inputStream.close();
return new String(data);
}
}
package cn.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
t.Context;
st.AndroidTestCase;
import android.util.Log;
import cn.service.FileService;
public class FileServiceTest extends AndroidTestCase {
private static final String TAG = "FileServiceTest";
public void testSave() throws Exception{
OutputStream outputStream = Context().openFileOutput(", Context.MODE_PRIVATE);
FileService.save(outputStream,"文件保存!");
}
public void testRead() throws Exception{
InputStream inputStream = Context().openFileInput(");
String content = ad(inputStream);
Log.i(TAG,content);
}
//读取其他应用的有文件
public void testPrivateRead() throws Exception{
File file = new File("/data/data/cn.file/");
InputStream inputStream = new FileInputStream(file);
String result = ad(inputStream);
Log.i(TAG,result);
}
//写入其它应用的文件
public void testPrivatewrite() throws Exception{
File file = new File("/data/data/cn.file/");
OutputStream outputStream = new FileOutputStream(file);
FileService.save(outputStream, "this is test");
}
}
package cn.file;
import java.io.InputStream;
import java.io.OutputStream;
import cn.service.FileService;
import android.app.Activity;
t.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FileActivity extends Activity {
private static final String TAG = "FileActivity";
private EditText filenameText;
private EditText contentText;
private TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
filenameText = (EditText) this.findViewById(R.id.filename);
contentText = (EditText) this.findViewById(t);
Button save = (Button) this.findViewById(R.id.save);
Button show = (Button) this.findViewById(R.id.show);
resultView = (TextView) this.findViewById(sult);
save.setOnClickListener(listener);
show.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button button = (Button)v;
String filename = Text().toString();
switch (Id()) {
case R.id.save:
String content = Text().toString();
int resId = R.string.sucess;
try {
OutputStream outputStream = FileActivity.this.openFileOutput(filename, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
FileService.save(outputStream,content);
}catch (Exception e) {
Log.i(String());
resId = ;
}
Toast.makeText(FileActivity.this, resId, Toast.LENGTH_LONG).show();
break;
case R.id.show:
try {
InputStream inputStream = FileActivity.this.openFileInput(filename);
String contents = ad(inputStream);
resultView.setText(contents);
} catch (Exception e) {
Log.i(String());
Toast.makeText(FileActivity.this, "读取失败!", Toast.LENGTH_LONG).show();
}
break;
}
}
};
}
package cn.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import cn.service.FileService;
import android.app.Activity;
t.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FileActivity extends Activity {
private static final String TAG = "FileActivity";
private EditText filenameText;
private EditText contentText;
private TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
filenameText = (EditText) this.findViewById(R.id.filename);
contentText = (EditText) this.findViewById(t);
Button save = (Button) this.findViewById(R.id.save);
Button show = (Button) this.findViewById(R.id.show);
resultView = (TextView) this.findViewById(sult);
save.setOnClickListener(listener);
show.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button button = (Button)v;
String filename = Text().toString();
File file =new ExternalStorageDirectory(),filename); //===new File("/sdcard"+filename);
switch (Id()) {
case R.id.save:
String content = Text().toString();
int resId = R.string.sucess;
ExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //sdkcar存在,并且可以读写
try {
/*OutputStream outputStream = FileActivity.this.openFileOutput(filename, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);*/
FileOutputStream outputStream = new FileOutputStream(file);
FileService.save(outputStream,content);
}catch (Exception e) {
Log.i(String());
resId = ;
}
Toast.makeText(FileActivity.this, resId, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(FileActivity.this, "SDCard不存在或者安全保护!", Toast.LENGTH_LONG).show();
}
break;
case R.id.show:
try {
/*InputStream inputStream = FileActivity.this.openFileInput(filename);*/
InputStream inputStream = new FileInputStream(file);
String contents = ad(inputStream);
resultView.setText(contents);
} catch (Exception e) {
Log.i(String());
Toast.makeText(FileActivity.this, "读取失败!", Toast.LENGTH_LONG).show();
}
break;
}
}
};
}
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>张三</name>
<age>20</age>
</person>
<person id="24">
<name>李四</name>
<age>21</age>
</person>
</persons>
package cn.service;
import java.util.ArrayList;
import java.util.List;
l.sax.Attributes;
l.sax.SAXException;
l.sax.helpers.DefaultHandler;
import cn.domain.Person;
public class XMLContentHandler extends DefaultHandler {
private List<Person> persons;
private Person person;
private String preTag;
@Override
public void startDocument() throws SAXException {
persons = new ArrayList<Person>();
}
public List<Person> getPersons() {
return persons;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(person != null){
String data = new String(ch,start,length);
if("name".equals(preTag)){
person.setName(data);
}else if("age".equals(preTag)){
person.setAge(new Short(data));
}
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(person != null && "person".equals(localName)){
persons.add(person);
person = null;
}
preTag = null;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if("person".equals(localName)){
person = new Person();
person.setId(new Value(0)));
}
preTag = localName;
}
}
package cn.service;
import java.io.InputStream;
import java.util.List;
l.parsers.SAXParser;
l.parsers.SAXParserFactory;
import cn.domain.Person;
public class SAXPersonService {
public static List<Person> readXml(InputStream inputStream) throws Exception {
SAXParserFactory spf = wInstance();
SAXParser saxParser = wSAXParser();
//设置解析器的相关特性,
//saxParser.setProperty("", true);
//表示开户命名空间特性
XMLContentHandler handler = new XMLContentHandler();
saxParser.parse(inputStream, handler);
inputStream.close();
Persons();
}
}
l;
import java.io.InputStream;
import java.util.List;
st.AndroidTestCase;
import android.util.Log;
import cn.domain.Person;
import cn.service.SAXPersonService;
public class SAXPersonServiceTest extends AndroidTestCase {
private static final String TAG = "SAXPersonServiceTest";
public void testReadXml() throws Exception {
InputStream inputStream = ClassLoader().getResourceAsStream(l");
List<Person> persons = adXml(inputStream);
for(Person person : persons){
Log.i(TAG, String());
}
}
}
package cn.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
l.parsers.DocumentBuilder;
l.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import cn.domain.Person;
public class DomPersonService {
public static List<Person> readXml(InputStream inStream) throws Exception {
List<Person> persons = new ArrayList<Person>();
DocumentBuilderFactory factory = wInstance();
DocumentBuilder builder = wDocumentBuilder();
Document document = builder.parse(inStream);
Element root = DocumentElement();
NodeList nodes = ElementsByTagName("person");
for(int i=0; i&Length();i++){
Element personElement = (Element)nodes.item(i);
Person person = new Person();
person.setId(new Attribute("id")));
NodeList childNodes = ChildNodes();
for(int j=0; j&Length();j++){
Node childNode = childNodes.item(j);
NodeType() == Node.ELEMENT_NODE){
Element childElement = (Element) childNode;
NodeName().equals("name")){
person.FirstChild().getNodeValue());
}else NodeName().equals("age")){
person.setAge(new FirstChild().getNodeValue()));
}
}
}
persons.add(person);
}
return persons;
}
}
package cn.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
lpull.v1.XmlPullParser;
import android.util.Xml;
import cn.domain.Person;
public class PullPersonService {
public static List<Person> readXml(InputStream inStream) throws Exception {
List<Person> persons = null;
XmlPullParser parser = wPullParser();
parser.setInput(inStream, "UTF-8");
int eventCode = EventType();
Person person = null;
while(eventCode != XmlPullParser.END_DOCUMENT){
switch (eventCode) {
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
Name().equals("person")){
person = new Person();
person.setId(new AttributeValue(0)));
}else if(person != null){
if("name".Name())){
person.Text());
}else if("age".Name())){
person.setAge(new Text()));
}
}
break;
case XmlPullParser.END_TAG:
if("person".Name()) && person != null){
persons.add(person);
person = null;
}
break;
}
eventCode = ();
}
return persons;
}
}
package cn.android;
import android.app.Activity;
t.Context;
t.SharedPreferences;
t.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class PreferencesActivity extends Activity {
private OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button button = (Button) v;
//Preferences(mode);//采用这种方式,保存名称为它PreferencesActivity
SharedPreferences pres = PreferencesActivity.this
.getSharedPreferences("soft", Context.MODE_PRIVATE);
switch (Id()) {
case R.id.setbutton:
String name = Text().toString();
String age = Text().toString();
Editor editor = pres.edit();
editor.putString("name", name);
editor.putInt("age", Integer.parseInt(age));
editormit();
Toast.makeText(PreferencesActivity.this, "保存成功!",
Toast.LENGTH_LONG).show();
break;
case R.id.showbutton:
String nameValue = String("name", "");
int ageValue = Int("age", 1);
result.setText("性名:"+ nameValue + "n年龄:" + ageValue);
break;
}
}
};
private EditText nameText;
private EditText ageText;
private TextView result;
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
Button setbutton = (Button) this.findViewById(R.id.setbutton);
Button showbutton = (Button) this.findViewById(R.id.showbutton);
nameText = (EditText) this.findViewById(R.id.name);
ageText = (EditText) this.findViewById(R.id.age);
result = (TextView) this.findViewById(sult);
setbutton.setOnClickListener(listener);
showbutton.setOnClickListener(listener);
SharedPreferences pres = PreferencesActivity.this
.getSharedPreferences("soft", Context.MODE_PRIVATE);
String nameValue = String("name", "");
int ageValue = Int("age", 1);
nameText.setText(nameValue);
ageText.setText(String.valueOf(ageValue));
}
}
package cn.log;
import android.app.Activity;
t.Context;
t.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
public class LogActivity extends Activity {
private static final String TAG = "LogActivity"; //shift+ctrl+x
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
try {
Context context = atePackageContext("cn.android", Context.CONTEXT_IGNORE_SECURITY);//构建其它应用
SharedPreferences pres = SharedPreferences("soft", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
String nameValue = String("name", "");
int ageValue = Int("age", 1);
Log.i(TAG, "性名:"+ nameValue + "n年龄:" + ageValue); //info级别
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
package cn.domain;
public class Person {
private Integer personId;
private String name;
public String toString() {
return "personId=" + personId + ", name=" + name + ", age="
+ age;
}
private Short age;
public Person(Integer personId, String name, Short age) {
super();
this.personId = personId;
this.name = name;
this.age = age;
}
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
}
package cn.service;
t.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseOpenHelper extends SQLiteOpenHelper {
private static final String DBNAME = "person";
private static final int VERSION = 3;
public DataBaseOpenHelper(Context context) {
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists person");
onCreate(db);
}
}
package cn.service;
import java.util.ArrayList;
import java.util.List;
t.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import cn.domain.Person;
public class PersonService {
private DataBaseOpenHelper dbOpenHelper;
public PersonService(Context context) {
dbOpenHelper = new DataBaseOpenHelper(context);
}
public void save() {
SQLiteDatabase database = WritableDatabase();
database.beginTransaction();
try {
SQL("insert into person(name,age)values(?,?)",
new Object[] {"张三32r", 20 });
SQL("insert into person(name,age)values(?,?)",
new Object[] {"李四", 21 });
database.setTransactionSuccessful();
} catch (SQLException e) {
}
dTransaction();
database.close();
}
public void save(Person person) {
SQLiteDatabase database = WritableDatabase();
SQL("insert into person(name,age)values(?,?)",
new Object[] { Name(), Age() });
database.close();
}
public void update(Person person) {
SQLiteDatabase database = WritableDatabase();
SQL(
"update person set name=?, age=? where personid = ?",
new Object[] { Name(), Age(),
PersonId() });
database.close();
}
public Person find(Integer id) {
SQLiteDatabase database = WritableDatabase();
Cursor cursor = database.rawQuery(
"select * from person where personid = ?",
new String[] { String.valueOf(id) });
if (veToNext()) {
return new Int(0), String(1),
Short(2));
}
database.close();
return null;
}
@SuppressWarnings("unused")
public void ids) {
if (ids.length > 0) {
StringBuilder sb = new StringBuilder();
for (Integer id : ids) {
sb.append('?').append(',');
}
sb.deleteCharAt(sb.length() - 1);
SQLiteDatabase database = WritableDatabase();
SQL(
"delete from person where personid in(" + sb + ")",
(Object[]) ids);
database.close();
}
}
public List<Person> getScrollData(int startResult, int maxRessult) {
SQLiteDatabase database = WritableDatabase();
Cursor cursor = database.rawQuery(
"select * from person limit ?,?",
new String[] { String.valueOf(startResult),
String.valueOf(maxRessult) });
List<Person> persons = new ArrayList<Person>();
while (veToNext()) {
persons.add(new Int(0), String(1),
Short(2)));
}
database.close();
return persons;
}
public long getCount() {
SQLiteDatabase database = WritableDatabase();
Cursor cursor = database.rawQuery("select count(*) from person", null);
if (veToNext()) {
Long(0);
}
database.close();
return 0;
}
}
package cn.service;
import java.util.ArrayList;
import java.util.List;
t.ContentValues;
t.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.domain.Person;
public class OtherPersonService {
private DataBaseOpenHelper dbOpenHelper;
public OtherPersonService(Context context) {
dbOpenHelper = new DataBaseOpenHelper(context);
}
public void save(Person person) {
SQLiteDatabase database = WritableDatabase();
ContentValues values = new ContentValues();
values.put("name", Name());
values.put("age", Age());
database.insert("person", "name", values); //当name有值才添加
database.close();
}
public void update(Person person) {
SQLiteDatabase database = WritableDatabase();
ContentValues values = new ContentValues();
values.put("name", Name());
values.put("age", Age());
database.update("person", values, "personid=?",new String[]{String.PersonId())});
database.close();
}
public Person find(Integer id) {
SQLiteDatabase database = WritableDatabase();
Cursor cursor = database.query("person", new String[]{"personid","name","age"}, "personid=?", new String[]{String.valueOf(id)}, null, null, null);
if (veToNext()) {
return new Int(0), String(1),
Short(2));
}
database.close();
return null;
}
public void ids) {
if (ids.length > 0) {
StringBuilder sb = new StringBuilder();
String[] strId = new String[ids.length];
for (int i =0;i<ids.length;i++) {
sb.append('?').append(',');
strId[i]=String.valueOf(ids[i]);
}
sb.deleteCharAt(sb.length() - 1);
SQLiteDatabase database = WritableDatabase();
database.delete("person", "personid in("+sb+")", strId);
database.close();
}
}
public List<Person> getScrollData(int startResult, int maxRessult) {
SQLiteDatabase database = WritableDatabase();
Cursor cursor = database.query("person", new String[]{"personid","name","age"}, null, null, null, null, "personid desc", startResult+","+maxRessult);
List<Person> persons = new ArrayList<Person>();
while (veToNext()) {
persons.add(new Int(0), String(1),
Short(2)));
}
database.close();
return persons;
}
public long getCount() {
SQLiteDatabase database = WritableDatabase();
Cursor cursor = database.query("person", new String[]{"count(*)"}, null, null, null, null, null);
if (veToNext()) {
Long(0);
}
database.close();
return 0;
}
}
package cn.db;
import java.util.List;
st.AndroidTestCase;
import android.util.Log;
import cn.domain.Person;
import cn.service.PersonService;
public class PersonServiceTest extends AndroidTestCase {
private static final String TAG = "PersonServiceTest";
public void testSaves() {
PersonService personService = new Context());
personService.save();
}
public void testSave() {
PersonService personService = new Context());
for(int i =0;i<10; i++){
personService.save(new Person(null, "张三"+i, (short) (20+i)));
}
}
public void testUpdate() {
PersonService personService = new Context());
Person person = personService.find(1);
person.setName("李四");
person.setAge((short) 21);
personService.update(person);
}
public void testFind() {
PersonService personService = new Context());
Person person = personService.find(1);
Log.i(TAG, String());
}
public void testDelete() {
PersonService personService = new Context());
personService.delete(1,2,3);
}
public void testGetScrollData() {
PersonService personService = new Context());
List<Person> persons = ScrollData(0, 20);
for(Person person : persons){
Log.i(String());
}
}
public void testGetCount() {
PersonService personService = new Context());
Log.i(TAG,""Count());
}
}
package cn.db;
import java.util.List;
st.AndroidTestCase;
import android.util.Log;
import cn.domain.Person;
import cn.service.OtherPersonService;
public class OtherPersonServiceTest extends AndroidTestCase {
private static final String TAG = "OtherPersonServiceTest";
public void testSave() {
OtherPersonService otherPersonService = new Context());
for(int i =0;i<10; i++){
otherPersonService.save(new Person(null, "张三"+i, (short) (20+i)));
}
}
public void testUpdate() {
OtherPersonService otherPersonService = new Context());
Person person = otherPersonService.find(1);
person.setName("李四");
person.setAge((short) 21);
otherPersonService.update(person);
}
public void testFind() {
OtherPersonService otherPersonService = new Context());
Person person = otherPersonService.find(1);
Log.i(TAG, String());
}
public void testDelete() {
OtherPersonService otherPersonService = new Context());
otherPersonService.delete(1,2,3);
}
public void testGetScrollData() {
OtherPersonService otherPersonService = new Context());
List<Person> persons = ScrollData(0, 20);
for(Person person : persons){
Log.i(String());
}
}
public void testGetCount() {
OtherPersonService otherPersonService = new Context());
Log.i(TAG,""Count());
}
}
package cn.db;
import java.util.HashMap;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import cn.service.PersonService;
public class dbActivity extends Activity {
private final static String TAG ="dbActivity";
private ListView listView;
private PersonService personService;
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) this.findViewById(R.id.listView);
personService = new PersonService(this);
/*List<Person> persons = ScrollData(0, 20);
List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
for (Person person : persons) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("personid", String.PersonId()));
map.put("name", Name());
map.put("age", String.Age()));
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(dbActivity.this, data,
R.layout.personitem,
new String[] { "personid", "name", "age" }, new int[] {
R.id.personid, R.id.name, R.id.age });*/
// ArrayAdapter<Integer>
// SimpleAdapter
// SimpleCursorAdapter
Cursor cursor = RawScrollData(0, 10);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.personitem, cursor, new String[]{"_id","name","age"}, new int[]{R.id.personid,R.id.name,R.id.age});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView listView = (ListView) parent;
HashMap<String, String> itemData = (HashMap<String, String>) ItemAtPosition(position);
// String personid = ("personid");
String personid = ("_id");
String name = ("name");
String age = ("age");
Log.i(Class().getName());
Toast.makeText(dbActivity.this, "personid="+personid+",name="+name+",age="+age, Toast.LENGTH_LONG).show();
Log.i(TAG,"result="+(position == id));
}
});
}
}
public class PersonContentProvider extends ContentProvider {
public int delete(Uri arg0, String arg1, String[] arg2) {
return 0;
}
public String getType(Uri uri) {
return null;
}
public Uri insert(Uri uri, ContentValues values) {
return null;
}
public boolean onCreate() {
return false;
}
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return null;
}
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
}
//常量UriMatcher.NO_MATCH表示不匹配任何路征的返回码:
UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//如果math()方法匹配content://cn.provider.personproider/person路征,返回匹配码为1
sMatcher.addURI("cn.provider.personprovider", "person", 1); //添加需要匹配uri,如果匹配就会返回匹配码
//如果math()方法匹配content://cn.provider.personproider/person/230路征,返回匹配码为2
sMatcher.addURI("cn.provider.personprovider", "person/#", 2);//#号为通配符
switch (sMatcher.match(Uri.parse("content://cn.provider.personproider/person/10"))) {
case 1:
break;
case 2:
break;
default: //不匹配
throw new IllegalArgumentException("Unknow Uri:"+ uri);
}
package cn.user;
import android.app.Activity;
t.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
public class ContentProviderUserActivity extends Activity {
private static final String TAG = "ContentProviderUserActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.main);
ContentResolver contentResolver = ContentResolver();
// Uri alluri = Uri
// .parse("content://cn.provider.personprovider/person");
// ContentValues values = new ContentValues();
// values.put("name", "张三");
// values.put("age", (short)20);
// contentResolver.insert(uri, values);
// Uri uri =
// Uri.parse("content://cn.provider.personprovider/person/1");
// contentResolver.update(uri, values, null, null);
// Uri uri = Uri
// .parse("content://cn.provider.personprovider/person/3");
// contentResolver.delete(uri, null, null);
// Cursor cursor = contentResolver.query(uri, new String[] {"personid",
// "name", "age" }, null, null, "personid desc");
// while (veToNext()) {
// Log.i(TAG,
// "personid=" + Int(0) + ",name="
// + String(1) + ",age="
// + Short(2));
// }
// cursor.close();
//查找联系人
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (veToNext()) {
int contractid = ColumnIndex(ContactsContract.Contacts._ID));
String name = ColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(TAG,
"contractid=" + contractid + ",name="
+ name);
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{String.valueOf(contractid)}, null);
StringBuilder sb = new StringBuilder();
veToNext()){
String phoneNumber = ColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append(phoneNumber).append(" ");
Log.i(String());
}
}
cursor.close();
}
}
本文发布于:2024-02-08 20:17:01,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170739490568816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |