在android中我试图在预定义的时间访问数据库。在我的应用程序中,它运行一个简单的Android服务,我希望在特定时间检查外部数据库(MySQL)。我正在使用java Web服务来访问数据库。
public class DBService extends Service {
private static final String SOAP_ACTION = "http://dbconnectivity.com/test/";
private static final String METHOD_NAME = "test";
private static final String NAMESPACE = "http://dbconnectivity.com";
private static final String URL = "http://10.0.2.2:8089/DB_Connectivity/services/SqlConnectivity?wsdl";
String str;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
//Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("time",0);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
str = s.toString();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(this, "Test "+str, Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
}
如何完成这项任务?
答案 0 :(得分:0)
查看AlarmManager类或检查this教程并在指定时间运行http poll到mysql服务器(你需要一些php脚本来返回MySQL的结果,因为android不支持直接数据库连接)