1. sharedpreference: stores simple information, such as the user name and password.
Package com. google. datastore. sharep;
Import android. app. Activity;
Import android. content. Context;
Import android. content. SharedPreferences;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import com. google. datastore. R;
Public class SharePreferences extends Activity {
Private EditText editUsername = null;
Private EditText editPassword = null;
Private Button bt = null;
SharedPreferences userInfo = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. sharepreferences );
EditUsername = (EditText) findViewById (R. id. username );
EditPassword = (EditText) findViewById (R. id. password );
Bt = (Button) findViewById (R. id. bt );
// String s = Environment. getExternalStorageDirectory (). toString ();
InitData ();
Bt. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
SharedPreferences. Editor editor = userInfo. edit ();
Editor. putString ("name", editUsername. getText (). toString ());
Editor. putString ("password", editPassword. getText (). toString ());
Editor. commit ();
}
});
}
//
Private void initData (){
UserInfo = getSharedPreferences ("user_info", Context. MODE_PRIVATE );
String username = userInfo. getString ("name ","");
String password = userInfo. getString ("password ","");
EditUsername. setText (username );
EditPassword. setText (password );
}
}
2. Network Storage
Package com.google.datastore.net;
Import java. io. BufferedInputStream;
Import java. io. InputStream;
Import java.net. HttpURLConnection;
Import java.net. URL;
Import org. apache. http. util. ByteArrayBuffer;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. widget. TextView;
Import com. google. datastore. R;
// If there is a problem, the idea is like this. After 2.3, you cannot access the network in the main thread.
Public class Net extends Activity {
Private TextView tvNet;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. netget );
TvNet = (TextView) findViewById (R. id. textNet );
New Thread (download). start ();
}
//
Runnable download = new Runnable (){
@ Override
Public void run (){
String msg = "";
Try {
URL url = new URL ("http://linux.chinaitlab.com/command/723482.html ");
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setReadTimeout (6*1000 );
Conn. setConnectTimeout (5*1000 );
InputStream is = conn. getInputStream ();
Log. d ("lixp", "is ========" + is );
BufferedInputStream bis = new BufferedInputStream (is );
ByteArrayBuffer baf = new bytes arraybuffer (100 );
Int current = 0;
While (current = bis. read ())! =-1 ){
Baf. append (byte) current );
}
Msg = new String (baf. toByteArray (), "UTF-8 ");
Log. d ("lixp", "msg ======" + msg );
}
Catch (Exception e ){
Msg = e. getMessage ();
Log. e ("lixp", "e =" + e );
}
// TvNet. setText (msg );
}
};
}
3. File Storage
Package com. google. datastore. filestore;
Import android. app. Activity;
Import android. content. Intent;
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. Toast;
Import com. google. datastore. R;
Public class FileActivity extends Activity {
Private EditText name;
Private EditText age;
Private FileService fileService;
Private Button saveButton;
Private Button readButton;
Private Button saveToSd;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. file );
Name = (EditText) findViewById (R. id. filename );
Age = (EditText) findViewById (R. id. content );
SaveButton = (Button) findViewById (R. id. save );
ReadButton = (Button) findViewById (R. id. read );
SaveToSd = (Button) findViewById (R. id. saveToSdCard );
FileService = new FileService (FileActivity. this );
SaveButton. setOnClickListener (listener );
SaveToSd. setOnClickListener (listener );
ReadButton. setOnClickListener (listener );
}
Private View. OnClickListener listener = new View. OnClickListener (){
@ Override
Public void onClick (View v ){
Button btn = (Button) v;
String filename = name. getText (). toString ();
String content = age. getText (). toString ();
Switch (btn. getId ()){
Case R. id. save:
Try {
FileService. save (filename, content );
Toast. makeText (FileActivity. this, R. string. fileSaveSuccess,
Toast. LENGTH_LONG). show ();
} Catch (Exception e ){
E. printStackTrace ();
Toast. makeText (FileActivity. this, R. string. fileSaveException,
Toast. LENGTH_LONG). show ();
}
Break;
Case R. id. saveToSdCard:
If (Environment. getExternalStorageState (). equals (
Environment. MEDIA_MOUNTED )){
Try {
FileService. saveToSd (filename, content );
Toast. makeText (FileActivity. this,
R. string. fileSaveSuccess, Toast. LENGTH_LONG). show ();
} Catch (Exception e ){
E. printStackTrace ();
Toast. makeText (FileActivity. this, R. string. fileSaveException,
Toast. LENGTH_LONG). show ();
}
} Else {
Log. e ("lixp", "Environment. getExternalStorageState () is not equals ..");
}
Break;
Case R. id. read:
// Error 5-16
FileService fileService = new FileService (getApplicationContext ());
Intent intent = getIntent ();
String fileName = intent. getStringExtra ("fileName ");
Log. d ("lixp", "fileService. read (fileName) =" + fileService. read (fileName ));
/* Try {
Content. setText (fileService. read (fileName ));
}
Catch (Exception e ){
E. printStackTrace ();
}*/
Break;
Default:
Break;
}
}
};
}
Package com. google. datastore. filestore;
Import java. io. ByteArrayOutputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import android. content. Context;
Import android. OS. Environment;
Import android. util. Log;
Public class FileService {
Private Context context;
Public FileService (Context context ){
Super ();
This. context = context;
}
/**
* Save the file
*/
Public void save (String fileName, String content ){
Try {
// Read and write data in private mode. The created file can only be accessed by this application.
FileOutputStream fileOutputStream = context. openFileOutput (fileName, Context. MODE_WORLD_READABLE );
FileOutputStream. write (content. getBytes ());
FileOutputStream. close ();
}
Catch (Exception e ){
Log. e ("lixp", "save () e ==============" + e );
}
}
/**
* Save the file to sdcard.
*/
Public void saveToSd (String fileName, String content ){
Try {
// File file = new File ("/mnt/sdcard"), fileName );
// Use the API provided by the system to obtain the directory of the SD card for different sdCard versions.
// Avoid conflicts between the name and the package.
File file = new File (Environment. getExternalStorageDirectory (), fileName );
FileOutputStream fileOutputStream = new FileOutputStream (file );
FileOutputStream. write (content. getBytes ());
FileOutputStream. close ();
}
Catch (Exception e ){
Log. e ("lixp", "saveToSd () e ==============" + e );
}
}
/**
* Reading file content
*/
Public String read (String fileName ){
Byte [] data = null;
Try {
FileInputStream fileInputStream = context. openFileInput (fileName );
//// Write the content read each time to the memory and obtain it from the memory.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
Byte [] buffer = new byte [1024];
Int len = 0;
// Keep reading as long as you have not finished reading
While (len = fileInputStream. read (buffer ))! =-1 ){
OutputStream. write (buffer, 0, len );
}
// Obtain the data stored in the memory in binary format
Data = outputStream. toByteArray ();
}
Catch (Exception e ){
Log. e ("lixp", "read () e ============" + e );
}
// Convert binary data to the corresponding string
Return new String (data );
}
}
4. SQLITE database storage
Package com. google. datastore. sqllite;
Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteDatabase. CursorFactory;
Import android. database. sqlite. SQLiteOpenHelper;
/* SQLiteOpenHelper: A helper class used to produce a database and manage the database version. This class is an abstract class. You need to inherit this class and implement this class.
OnCreate (SQLiteDatabase): This method is called during the first database production. Generally, database tables are produced in this method.
OnUpgrade (SQLiteDatabase, int, int): When the database needs to be upgraded, the Android system will actively call this method. In general, we delete database tables and create new database tables in this method. Of course, whether other operations are required depends on the needs of the application.
OnOpen (SQLiteDatabase): This is the callback function when the database is opened and is generally not used.
The SQLiteDatabase object is returned by calling the program method.
When the getWritableDatabase () or getReadableDatabase () method of this class is called in the program, if there is no data at that time, the Android system will automatically produce a database. After the database is used, remember to call the close () method to close the database.
*/
Public class DbOpenHelper extends SQLiteOpenHelper {
Public static final String TABLE_NAME = "fb ";
Public static final String ID = "_ id ";
Public static final String COUNTRY = "country ";
Public static final String CODE = "code ";
// Constructor
Public DbOpenHelper (Context context, String name, CursorFactory factory,
Int version ){
Super (context, name, factory, version );
}
@ Override
Public void onCreate (SQLiteDatabase db ){
Db.exe cSQL ("create table if not exists"
+ TABLE_NAME + "("
+ ID + "integer primary key ,"
+ COUNTRY + "VARCHAR ,"
+ CODE + "INTEGER )"
);
}
// Upgrade the database
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
Db.exe cSQL ("drop table if exists" + TABLE_NAME );
OnCreate (db );
}
}
Package com. google. datastore. sqllite;
Import android. app. Activity;
Import android. OS. Bundle;
Import com. google. datastore. R;
Public class DbActivity extends Activity {
DbOpenHelper helper = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
/**
* Obtain the instance of SQLiteDatabase.
*/
/* Helper = new DbOpenHelper (this, FB, null, version );
// If no database exists, it is automatically created
SQLiteDatabase db = helper. getWritableDatabase ();*/
}
/**
* Insert data
*/
/* ContentValues values = new ContentValues ();
Values. put (DBHelper. COUNTRY, "China ");
Values. put (DBHelper. CODE, 86 );
Db. insert (DBHelper. TB_NAME, DBHelper. ID, values );
*//**
* Modify data
*//*
Db. insert (DBHelper. TB_NAME, DBHelper. ID, null );
Values. clear ();
Values. put (DBHelper. COUNTRY, "Italy ");
Values. put (DBHelper. CODE, 39 );
Db. update (DBHelper. TB_NAME, values, DBHelper. ID + "= 2", null );
*//**
* ExecSQL executes the SQL language
*//*
Db.exe cSQL ("insert"
+ DBHelper. TB_NAME + "("
+ DBHelper. COUNTRY + ","
+ DBHelper. CODE + ") VALUES"
+ "('Honduras, 504 )");
*//**
* Query data
*//*
Cursor c = db. query (DBHelper. TB_NAME, null,
DBHelper. CODE + "DESC ");
// Delete data
Db. delete (DBHelper. TB_NAME, null, null );
*/
}