Push_Notification在Android上的WebServices [英] Push_Notification on Android with WebServices
问题描述
我有一种情况下,它总是与Web服务检查。如果在数据库中的变化那么它应该弹出一个警报消息。我已经做while循环。
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.main); Push_notificaton();
}公共无效Push_notificaton(){
而(真)
{
尝试
{
SoapObject请求=新SoapObject(http://tempuri.org/,的getMessage);SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = TRUE;
envelope.setOutputSoapObject(请求); // Web方法调用
HttpTransportSE androidHttpTransport =新HttpTransportSE(http://180.215.66.194/WebService.asmx);
androidHttpTransport.call(http://tempuri.org/+的getMessage,信封);
//得到响应
SoapPrimitive响应=(SoapPrimitive)envelope.getResponse(); //响应对象可通过其名称进行检索:result.getProperty(对象名);
字符串消息=(字符串)response.toString();
如果(!currentMsg.equals(消息))
{
currentMsg =消息;
AlertDialog.Builder alertDialogBuilder =新AlertDialog.Builder(本); //设置标题
alertDialogBuilder.setTitle(您的标题); //设置对话框消息
alertDialogBuilder
.setMessage(单击是退出!)
.setCancelable(假)
.setPositiveButton(是,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
//如果单击此按钮,关闭
// 目前的活动
MessagesRequestActivity.this.finish();
}
})
.setNegativeButton(否,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
//如果单击此按钮,只要关闭
//对话框,什么也不做
dialog.cancel();
}
}); //创建警报对话框
AlertDialog alertDialog = alertDialogBuilder.create(); // 展示下
alertDialog.show();
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
}
Toast.makeText(这一点,消息,Toast.LENGTH_LONG).show(); }
赶上(例外五)
{
e.printStackTrace();
}
但在这里我无法获得,因为连续循环播放的输出。让我知道任何其他方式从该克服的。
-
不要做在UI线程的web服务的任何请求。移动你的code到服务或AsyncTask的。前者是更好的。
-
这方法是耗能的。至少,尽量让睡眠(有时)的请求。
I have a scenario which always checking with the web services. If there is a changes in the DataBase then it should pop-up a alert message . I have done With a 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();
}
But here I'm unable to get the output because of continuous looping. Let me know any other way to overcome from this.
Don't do any request to the web-service in ui-thread. Move your code to Service or AsyncTask. The former is better.
This approach is energy consuming. At least, try to make requests with sleep(someTime).
这篇关于Push_Notification在Android上的WebServices的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!