我在我的项目中工作,该项目具有SQLLIT并与SQL-server
交互。在我的服务器中,我想知道:如何创建一个侦听我的sqllite
表的服务并检查是否有任何表中的新记录?它使用ksoap2将此记录传递给服务器。
此外,我需要首先检查我的服务是否有可用的网络。
答案 0 :(得分:0)
我相信你的SQLite表会从一个活动变化(不知何故需要某种用户交互)。所以我建议采用以下方法:
使用一些侦听器创建一个SQLite表,只要表执行任何CRUD操作,就会调用这些侦听器。
public class YourSqliteTable {
private TableListener listener;
public void insert(Object urData){
//insert data here to sqlite table
// call the listener
listener.onInsert(urData);
}
}
public interface TableListener{
public void onInsert(Object tableRow);
}
2.创建一个处理soap消息并侦听SQLite表的服务:
public class SoapService extend Service implements TableListener{
private HanlderThread soapThread;
private Handler soapHandler;
public void onCreate(){
soapThread = new HandlerThread("handler_thread");
soapThread.start();
soapHandler = new Handler(soapThread.getLooper){
public void handleMessage(Message msg) {
switch(msg.what){
case(1)://send data to server
HttpTransportSE transporter = new HttpTransportSE(Server.TIGA_SERVER_IP
+ SOAP_URL);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope sEnv = new SoapSerializationEnvelope(SoapEnvelope.VER11);
try {
request.addProperty("data", msg.obj);
sEnv.dotNet = true;
sEnv.setOutputSoapObject(request);
transporter.call(NAMESPACE + METHOD_NAME, sEnv);
SoapObject response = (SoapObject) sEnv.getResponse();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
} finally{
transporter.reset();
}
break;
}
}
}
@Override
public void onInsert(Object tableRow){
Message msg = new Message();
msg.what = 1;
msg.obj = tableRow;
soapHandler.sendMessage(msg);
}
}
将用户活动绑定到SoapService,并且在建立绑定时(调用onServiceConnected)将服务注册为SQLite表的侦听器。
检查互联网连接是否可用:
public boolean isNetworkOn(){
ConnectivityManager conMan = (ConnectivityManager)
getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNet = conMan.getActiveNetworkInfo();
if(activeNet == null)
return false;
if(activeNet.isAvailable() && activeNet.isConnected())
return true;
return false;
}