Service
一、创建Service
1, 定义一个类, 继承Service
2, 重写父类的方法, onBind() — 必须重写的方法
3, 在清单文件中, 注册Service
二、Service的生命周期以及启动方式
1.启动方式
生命周期:onCreate() — onStartCommand() — onDestroy()
开启服务:startService()
停止服务:stopService()
2.
绑定方式:onCreate() – onBind() — onUnbind() — onDestroy()
绑定服务:bindService()
解除绑定:unbindService()
三、Service执行数据库操作(绑定方式)
效果图
布局文件
// An highlighted block
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="添加"
android:onClick="insert"
/>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="修改"
android:onClick="update"
/>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="删除"
android:onClick="delete"
/>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="查询"
android:onClick="select"
/>
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="0dp"></ListView>
</LinearLayout>
listview布局文件
// An highlighted block
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="20dp"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="20dp"
/>
</LinearLayout>
Java代码
// An highlighted block
public class MainActivity extends AppCompatActivity {
ListView listView;
MyAdapter myAdapter;
MyHeleper myHeleper;
SQLiteDatabase db;
Intent intent;
MyServer myServer;
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myServer = ((MyServer.MyBinder) service).getMyserver();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.lv);
myHeleper = new MyHeleper(this);
db = myHeleper.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("uname","王五");
contentValues.put("uage","20");
db.insert("user",null,contentValues);
intent = new Intent(MainActivity.this,MyServer.class);
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
public void insert(View view){
User user = new User("张三",21);
myServer.insertData(user,db);
}
public void delete(View view){
myServer.deleteData("王五",db);
}
public void update(View view){
myServer.updateData("张三",db);
}
public void select(View view){
ArrayList<User> users = myServer.selectData("张三", db);
myAdapter = new MyAdapter(MainActivity.this,users);
listView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
Service类代码
// An highlighted block
public class MyServer extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public class MyBinder extends Binder{
public MyServer getMyserver(){
return MyServer.this;
}
}
public void insertData(User user,SQLiteDatabase db){
ContentValues contentValues = new ContentValues();
contentValues.put("uname",user.getName());
contentValues.put("uage",user.getAge());
db.insert("user",null,contentValues);
Toast.makeText(this, "添加成功!", Toast.LENGTH_SHORT).show();
}
public void updateData(String name,SQLiteDatabase db){
ContentValues contentValues = new ContentValues();
contentValues.put("uname","李四");
int update = db.update("user",contentValues,"uname = ?", new String[]{name});
if (update == 0){
Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "更新成功",Toast.LENGTH_SHORT).show();
}
}
public void deleteData(String name,SQLiteDatabase db){
int delete = db.delete("user", "uname = ?", new String[]{name});
if (delete == 0){
Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "删除成功",Toast.LENGTH_SHORT).show();
}
}
public ArrayList<User> selectData(String name,SQLiteDatabase db){
ArrayList<User> users = new ArrayList<>();
Cursor cursor = db.query("user", null, null, null, null, null, null);
while (cursor.moveToNext()){
String uname = cursor.getString(cursor.getColumnIndex("uname"));
int uage = cursor.getInt(cursor.getColumnIndex("uage"));
User user = new User(uname,uage);
users.add(user);
}
return users;
}
}
创建数据库
// An highlighted block
public class MyHeleper extends SQLiteOpenHelper {
public MyHeleper(Context context){
super(context,"serverK",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user (uname varchar(10),uage integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
lisview适配器
// An highlighted block
public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<User> users;
public MyAdapter(Context context, ArrayList<User> users) {
this.context = context;
this.users = users;
}
@Override
public int getCount() {
return users.size();
}
@Override
public Object getItem(int position) {
return users.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
convertView = View.inflate(context,R.layout.layout_list,null);
viewHolder = new ViewHolder();
viewHolder.textView1 = convertView.findViewById(R.id.tv1);
viewHolder.textView2 = convertView.findViewById(R.id.tv2);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView1.setText("姓名:"+users.get(position).getName());
viewHolder.textView2.setText("年龄:"+users.get(position).getAge());
return convertView;
}
class ViewHolder{
TextView textView1;
TextView textView2;
}
}