Android Serviceコンポーネント導入サービス
9314 ワード
2018/6/22 CZ 13:45 c.~
Android
最近では、Androidの下で、サーバ-クライアントモードを持つアプリをどのように完成させるかを構想しています.
サーバ上でSQLiteを実行し、クライアントはリモートアクセスを通じてSQLiteデータを取得し、一連のアクセスを行い、削除・変更操作を行う.
構想プロセス:
1.サーバ・モードを配備します.Androidに付属のサービスコンポーネントを使用して、リモートサービスを提供できます.
2.クライアントを配備します.ServiceConnectionを使用して、ServiceのコールバックOnbind()を実現する方法.
具体的なプロセス:
1.Service
サービス・エンドを導入する際に、いくつかの問題が発生しました.
1.サービスとは何ですか.
androidモードで持参した4つのコンポーネントの1つです.プロセスと似ています.作成して破棄しないとandroidのバックグラウンドでずっと実行されます.
2.サービスの作成方法
3.サービスを開く方法
---------サービスを開くには2つの方法があります
-----------1.BindService()//unbind()
BindServiceではActivityとServiceのデータ交換、通信などのリモート操作が可能です.
----------2. StartService()//stopservice()
4.ActivityインタフェースとRemote-serviceの間でどのように対話するか
サービス---MainActivity配備ダイレクトシールコード
Services-SQLite配備//コード貼付
Services-Dataserviceの導入//
AIDLインタフェース
IStudentImpl実装インタフェースクラス
!!!!!!!!!!!!!!!!最后に登录リストは必ず书きます!!!!!!!!!!!
//AIDLについては次編で詳しく述べる
Android
最近では、Androidの下で、サーバ-クライアントモードを持つアプリをどのように完成させるかを構想しています.
サーバ上でSQLiteを実行し、クライアントはリモートアクセスを通じてSQLiteデータを取得し、一連のアクセスを行い、削除・変更操作を行う.
構想プロセス:
1.サーバ・モードを配備します.Androidに付属のサービスコンポーネントを使用して、リモートサービスを提供できます.
2.クライアントを配備します.ServiceConnectionを使用して、ServiceのコールバックOnbind()を実現する方法.
具体的なプロセス:
1.Service
サービス・エンドを導入する際に、いくつかの問題が発生しました.
1.サービスとは何ですか.
androidモードで持参した4つのコンポーネントの1つです.プロセスと似ています.作成して破棄しないとandroidのバックグラウンドでずっと実行されます.
2.サービスの作成方法
class MyService extends Service{} // Service
3.サービスを開く方法
---------サービスを開くには2つの方法があります
-----------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() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO
//mBound=false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Service
Log.d("Client-Service","onServiceConnected");
ist=IStudent.Stub.asInterface(service);
Log.d("ist"," ="+ist);
}
};
サービス---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() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mBound=false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.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 exception
e.printStackTrace();
}
mBound=true;
}
};
@Override
protected 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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (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;
}
}
}
Services-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
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
ContentValues 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);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("alter table person add account varchar(20)");
}
}
Services-Dataserviceの導入//
public class MyService extends Service{
public static SQLiteCreateHelper helper;// , , ,
IStudentImpl istu = new IStudentImpl();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
helper=new SQLiteCreateHelper(MainActivity.context);
return istu;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("Server-Service","onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d("Server-Service","onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.d("Server-Service","onDestroy");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.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;
}
@Override
public 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;
}
@Override
public String selectPass(String id) throws RemoteException {
// TODO Auto-generated method stub
String 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については次編で詳しく述べる