1

我正在创建应用程序,它只是从服务器中提取消息显示通知并始终运行服务。

我需要,始终在后台运行的通知服务 - 每 2 分钟一次。该服务调用Web服务检查,是否有任何新的消息?

所以我只是简单的服务调用我的登录 Web 服务来检查工作。但它挂起并显示未响应消息。意外关闭。

我确信必须有另一种方法来调用 Web 服务 Using Service 但我找不到正确的方法。请告诉我我应该如何在后台调用网络服务来检查带有“app.Service”的消息。谢谢

enter code here

public class NotifyService extends Service {
private Long counter = 0L;
private Timer timer = new Timer();

private static String SOAP_ACTION = "http://tempuri.org/Validation";
private static String METHOD_NAME = "Validation";
private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "**********************/Trackingservice.asmx?WSDL";

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this, "Service created ...", Toast.LENGTH_LONG).show();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
        try{
        login();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            Toast.makeText(this, "ERROR ..."+e, Toast.LENGTH_LONG).show();
        }
}
//  @Override
//  public int onStartCommand(Intent intent, int flags, int startId) {
//      try{
//          login();
//          }
//          catch(Exception e)
//          {
//              e.printStackTrace();
//              Toast.makeText(this, "ERROR ..."+e, Toast.LENGTH_LONG).show    ();
//          }
//      return Service.START_NOT_STICKY;
//    }

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}

public void login() {

    // Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    // Use this to add parameters
    request.addProperty("UserName", "omega");
    request.addProperty("Password", "omega");

    // Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true; // for using Dot Net Web Service

    try {

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        // this is the actual part that will call the webservice
        androidHttpTransport.call(SOAP_ACTION, envelope);
        // Get the SoapResult from the envelope body.
        SoapObject result = (SoapObject) envelope.bodyIn;

        if (result != null) {
            // Get the first property and change the label text
            String temp = result.getProperty(0).toString();
            if (new Boolean(temp)) {
                Toast.makeText(getApplicationContext(), "Login Success",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Login Fails, Please check username and password",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    "No Response, Service Not availble", Toast.LENGTH_LONG)
                    .show();
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(
                getApplicationContext(),
                " Network Exception : " + e
                        + "Please check network connectivity.",
                Toast.LENGTH_LONG).show();

    }

    }
}
4

1 回答 1

0

您可以使用计时器来满足您的要求作为解决方案之一::

autoUpdate = new Timer();
    autoUpdate.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    //call your service from here
                }
            });
        }
    }, 0, 5000);//here provide the duration

希望这可以帮助 :)

于 2012-11-08T09:06:29.930 回答