我正在创建仅从服务器拉取消息的应用程序显示通知并始终运行服务。
我需要, 始终在后台运行的通知服务 - 每2分钟一次。该服务调用Web服务进行检查,是否有新的消息?
所以我做了简单的服务,调用我的登录Web服务来检查工作。 但它挂起并显示不响应消息。出乎意料地关闭了。
我确信必须有另一种方式来调用Web服务使用服务但是我无法找到当前的方式。 请, 告诉我如何在后台调用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 :(得分: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
希望这会有所帮助:)