0

在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();
}

}

这个任务怎么做?

4

1 回答 1

0

查看 AlarmManager 类或查看教程并在指定时间运行 http poll 到 mysql 服务器(您需要一些 php 脚本从 MySQL 返回结果,因为 android 不支持直接 DB 连接)

于 2012-07-10T09:52:04.150 回答