Android(安卓)Service组件 部署服务端
2018/6/22 CZ 13:45 c.~
Android
最近在构想,在Android下,如何去完成一个APP,它具有服务器-客户端模式。
服务器上运行SQLite,客户端通过远程访问去获取SQLite数据,进行一系列的访问,增删改查操作。
构想过程:
1.部署服务器模式。使用Android自带的Service组件,可以提供远程服务。
2.部署客户端。使用ServiceConnection 去实现Service的回调Onbind()的方法。
具体过程:
1.Service
在部署服务端时,遇到了几个问题。
1.Service是什么。
是android模式下自带的四大组件之一。它跟进程类似。一旦创建,不去销毁,就会一直运行在android的后台。
2.如何创建service
class MyService extends Service{} //继承Service即可
3.如何开启service
------------开启service有两种方法
-----------1.BindService()//unbind()
bindService 可以使Activity和Service进行数据交换,通信等远程操作。
bindservice(intent,serviceConnection,int falgs) //一般多用于隐式意图
----------2. StartService()//stopservice()
startservice(intent)//无法进行通信及数据交换
4.activity界面和Remote-service之间是如何进行交互的
private ServiceConnection connection =new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO 解除连接的操作//mBound=false;}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO 获取远程ServiceLog.d("Client-Service","onServiceConnected");ist=IStudent.Stub.asInterface(service);Log.d("ist","学生对象的内存地址="+ist);}};
Service---MainActivity部署 直接贴代码
public class MainActivity extends Activity implements OnClickListener{ Button btn1;Button btn2;IStudent ist;boolean mBound=false;IStudentImpl iStudentImpl;public static Context context;//public static SQLiteCreateHelper helper;private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubmBound=false;}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubLog.i("studentInfo","onServiceConnected");ist=IStudent.Stub.asInterface(service);Log.i("studentInfo","学生对象内存地址="+ist);try {//SQLiteCreateHelper helper =new SQLiteCreateHelper(MainActivity.this);//sqL.getWritableDatabase(); boolean b =ist.selectID("0112"); Log.d("个人密码:","是否正确="+b);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}mBound=true;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn1=(Button)findViewById(R.id.button1);btn2=(Button)findViewById(R.id.button2);btn1.setOnClickListener(this);btn2.setOnClickListener(this);context=MainActivity.this;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.button1: Intent intent = new Intent(MainActivity.this,MyService.class); bindService(intent, connection, BIND_AUTO_CREATE);break;case R.id.button2:if(mBound){unbindService(connection);mBound=false;}break;default:break;}}}
Service-SQLite部署//贴代码
public class SQLiteCreateHelper extends SQLiteOpenHelper{ public static Cursor cursor;public SQLiteCreateHelper(Context context) {super(context,"subject1.db",null,5);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubContentValues values=new ContentValues();onOpen(db);String sysName="root";String sysID="0112";String sysPosition="秘书";String syspassword="123456";values.put("stuID", sysID);values.put("stuName", sysName);values.put("stuPosition", sysPosition);values.put("stupassword", syspassword);// TODO Auto-generated method stub//学生信息表,db.execSQL("create table stuInfo ( stuID varchar(20) primary key,"+"stuName varchar(20) not null,"+"stuPosition varchar(50) not null,"+"stupassword varchar(15) not null);");//教师信息表db.execSQL("create table teachInfo (teaID varchar(20) not null primary key ,"+"teaName varchar(20) not null ,"+"teaPosition varchar(50) not null,"+"teapassword varchar(15) not null);");//管理员信息表db.execSQL("create table sysInfo ( sysID varchar(20) primary key ,"+"sysName varchar(20) not null ,"+"sysPosition varchar(50) not null,"+"syspassword varchar(15) not null);");//课程表信息db.execSQL("create table subject ( subID varchar(15) primary key ,"+"subName varchar(25) not null,"+"subTotalStu integer );");//学生选课表+成绩+课堂评价+出勤情况db.execSQL("create table stuSelectSub ( stuID varchar(20) not null,"+"subID varchar(15) not null,"+"subName varchar(25) not null,"+"stuPosition varchar(50) not null,"+"commandS1 integer ,"+"commandS2 integer ,"+"commandS3 integer,"+"commandS4 integer,"+"workScroe integer ,"+"totduty integer not null,"+"subevalute varchar(20) not null,FOREIGN KEY(stuID) REFERENCES stuInfo(stuID),FOREIGN KEY(subID) REFERENCES subject(subID));");db.execSQL("create table stuInfo2 (stuID varchar(20) primary key,"+"Nicheng varchar(20) not null,"+"personInfo varchar(50) not null);");db.execSQL("create table teaInfo2 (teaID varchar(20) primary key,"+"Nicheng varchar(20) not null,"+"personInfo varchar(50) not null);");db.insert("stuInfo",null, values);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stubdb.execSQL("alter table person add account varchar(20)");}}
Service-Dataservice部署//
public class MyService extends Service{ public static SQLiteCreateHelper helper;//全局变量,如果不创建,会导致在访问数据库时,无法操作IStudentImpl istu = new IStudentImpl();@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubhelper=new SQLiteCreateHelper(MainActivity.context);return istu;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.d("Server-Service","onCreate");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.d("Server-Service","onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubLog.d("Server-Service","onDestroy");super.onDestroy();}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubLog.d("Server-Service","onUnbind");return super.onUnbind(intent);} }
AIDL 接口
package com.example.servicetest01;//包名一定要跟原本的包名一样,否则gen无法自动生成.Java文件interface IStudent{ boolean selectID(String id); String selectPass(String id);}
IStudentImpl 具体实现接口类
public class IStudentImpl extends IStudent.Stub{ //private SQLiteCreateHelper helper;private Context context;//private Cursor cursor;public IStudentImpl(){ System.out.println("数据库连接成功");//this.helper=MyService.helper;this.context=MainActivity.context;}@Overridepublic boolean selectID(String id) throws RemoteException {// TODO Auto-generated method stub SQLiteDatabase db=MyService.helper.getWritableDatabase(); SQLiteCreateHelper.cursor=db.rawQuery("select stuID from stuInfo where stuID=?",new String[]{id}); boolean result=SQLiteCreateHelper.cursor.moveToNext(); SQLiteCreateHelper.cursor.close(); db.close(); return result;}@Overridepublic String selectPass(String id) throws RemoteException {// TODO Auto-generated method stubString pass="";SQLiteDatabase db=MyService.helper.getWritableDatabase();SQLiteCreateHelper.cursor=db.rawQuery("select stupassword from stuInfo where stuID=?", new String[]{id});if(SQLiteCreateHelper.cursor==null){Toast.makeText(context, "无数据", Toast.LENGTH_LONG).show();}else {while(SQLiteCreateHelper.cursor.moveToNext()){pass = SQLiteCreateHelper.cursor.getString(SQLiteCreateHelper.cursor.getColumnIndex("stupassword"));}}SQLiteCreateHelper.cursor.close();db.close();return pass;}}
!!!!!!!!!!!!!!!!最后注册清单一定要写!!!!!!!!!!!!!!!!
//设置action可以使其他app也能调用此Impl
//服务端部署完成 //关于AIDL在下一篇详讲
更多相关文章
- Android(安卓)uiautomator实例使用
- android中使用MediaRecorder进行视频录制笔记
- Android(安卓)Camera使用Matrix进行滑动特效变换
- Android之使用Android-query框架进行开发(一)
- Android(安卓)自定义权限 ( )
- Android轻量级存储源码分析
- Android使用Handler实时更新UI
- Android(安卓)onTouchEvent, onClick及onLongClick的调用机制
- Android(安卓)第二课——命令行基本操作