1

我有一个总是检查 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();
        }

但是在这里,由于连续循环,我无法获得输出。让我知道任何其他方法来克服这一点。

4

2 回答 2

2

您应该使用云服务,它会自动将通知推送到您的 android 应用程序,这可能会触发特定的实现或处理。

你应该看看GCM (Google Cloud Messaging)

什么是 GCM?

Google Cloud Messaging for Android (GCM) 是一项服务,可让您将数据从服务器发送到用户的 Android 设备。这可能是一条轻量级消息,告诉您的应用程序要从服务器获取新数据(例如,朋友上传的电影),也可能是一条包含高达 4kb 有效负载数据的消息(因此即时消息等应用程序可以直接消费消息)。上面的链接向您展示了使用 google 配置应用程序所需的步骤。

显然,请通过本教程了解 mySQL 和 php,它成功设置了一个供 GCM 使用的应用程序

为什么要使用 GCM 及其优势?

  • 它将使您摆脱不断检查服务器以查找数据库更改的麻烦。这会导致您的应用程序出现巨大的性能问题。
  • 基本上,你要做的就是在数据库发生变化时自动调用一段代码。更可行的是,不仅在修改数据时,这也可以在其他地方使用。所以你不必费心检查服务器每次。Google 对此感到头疼。

如果您使用的是 asp.net。您将使用这段代码来触发推送

请记住,GCM 实现非常简单,可以快速实现

于 2013-03-08T12:45:18.440 回答
1
  1. 不要在 ui-thread 中对 web-service 做任何请求。将您的代码移动到 Service 或 AsyncTask。前者更好。

  2. 这种方法很耗能。至少,尝试使用 sleep(someTime) 发出请求。

于 2013-03-08T12:44:21.217 回答