2012-11-08 65 views
1

我正在創建應用程序,只是從服務器拉消息顯示通知,並始終運行服務。WebService在android android.app.Service沒有響應

我需要, 始終在後臺運行的通知服務 - 每2分鐘一次。該服務調用Web服務來檢查,是否有任何新的消息?

所以我只是簡單的服務調用我的登錄Web服務來檢查工作。 但它掛起並顯示沒有響應的消息。意外關閉。

我確定必須有另一種方式來調用Web服務使用服務但我無法找到正確的方式。 please, 告訴我如何在後臺調用web服務來檢查「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(); 

    } 

    } 
} 

回答

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 

希望這有助於:)