我有一个总是检查 Web 服务的场景。如果数据库有变化,那么它应该会弹出一个警报消息。我已经完成了 WHILE LOOP。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Push_notificaton();
}
public void Push_notificaton(){
while(true)
{
try
{
SoapObject request = new SoapObject("http://tempuri.org/","getMessage");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
//Web method call
HttpTransportSE androidHttpTransport = new HttpTransportSE("http://180.215.66.194/WebService.asmx");
androidHttpTransport.call("http://tempuri.org/"+ "getMessage", envelope);
//get the response
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
//the response object can be retrieved by its name: result.getProperty("objectName");
String message = (String)response.toString();
if(!currentMsg.equals(message))
{
currentMsg=message;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MessagesRequestActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
}
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
e.printStackTrace();
}
但是在这里,由于连续循环,我无法获得输出。让我知道任何其他方法来克服这一点。