主Activity:主要就是监听事件启动Service
package com.example.jserver;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity { private Button onSer; private Button stopSer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); onSer=(Button) findViewById(R.id.ser); stopSer=(Button) findViewById(R.id.stopSer); ButtonClick click=new ButtonClick(); onSer.setOnClickListener(click); stopSer.setOnClickListener(click); } class ButtonClick implements OnClickListener{ @Override public void onClick(View view) { Button but=(Button) view; Intent intent=new Intent(); intent.setClass(MainActivity.this, JService.class); if(R.id.ser==but.getId()){ Toast.makeText(MainActivity.this, "进来了On", Toast.LENGTH_SHORT).show(); startService(intent); }else{ Toast.makeText(MainActivity.this, "进来了stop", Toast.LENGTH_SHORT).show(); stopService(intent); } } }}
Service:主要是响应点击事件以及创建数据库
package com.example.jserver;import java.io.Serializable;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Service;import android.content.ContentValues;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.IBinder;public class JService extends Service{ @Override public IBinder onBind(Intent intent) { System.out.println("------>onBind"); return null; } @Override public void onCreate() { super.onCreate(); System.out.println("------>onCreate"); } @Override public void onDestroy() { System.out.println("------>onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("------>onStartCommand"); JLite lite=new JLite(JService.this, "ser_user", null, 1); SQLiteDatabase db=lite.getReadableDatabase(); lite.onCreate(db); for (int i = 10; i < 100; i++) { ContentValues values=new ContentValues(); values.put("name", "吕月秋"+i); values.put("address", "东湖"+i); db.insert("ser_user", null, values); } Cursor cursor=db.rawQuery("select * from ser_user", null); List
安卓数据库:
package com.example.jserver;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class JLite extends SQLiteOpenHelper { public JLite(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { System.out.println("---->createDataBase"); db.execSQL("drop table IF exists ser_user"); db.execSQL("create table ser_user(id integer primary key autoincrement , name text,address text)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("---->onUpgrade"); }}
处理需要显示的Acivity
package com.example.jserver;import java.util.List;import java.util.Map;import android.app.ListActivity;import android.content.Intent;import android.os.Bundle;import android.widget.SimpleAdapter;public class ShowActivity extends ListActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.toshow); Intent intent=getIntent(); List> list=(List >) intent.getSerializableExtra("list"); SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.show, new String[]{"id","name","address"}, new int[]{R.id.id,R.id.name,R.id.address}); setListAdapter(adapter); } }
主要布局文件man.xml:
处理LIST数据显示的布局文件toshow.xml
显示数据文件show.xml
关于Activity的配置文件