【问题标题】:Push_Notification on Android with WebServices使用 Web 服务在 Android 上推送通知
【发布时间】:2013-03-08 12:23:26
【问题描述】:

我有一个总是检查网络服务的场景。如果数据库有变化,那么它应该会弹出一个警报消息。我已经完成了 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();
        }

但是由于连续循环,我无法获得输出。让我知道任何其他方法可以克服这个问题。

【问题讨论】:

    标签: java android asp.net


    【解决方案1】:

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

    你应该看看GCM (Google Cloud Messaging)

    什么是 GCM?

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

    显然,通过this tutorial for mySQL 和 php 成功设置了一个供 GCM 使用的应用程序

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

    • 这将使您无需不断检查服务器以查找数据库更改。这会导致您的应用出现巨大的性能问题。
    • 基本上,您要做的就是在数据库发生变化时自动调用一段代码。更可行的是,不仅可以修改数据,还可以在其他地方使用。这样您就不必费心了每次都检查服务器。Google 很头疼。

    如果您使用的是asp.net。您将使用这段代码来trigger the push

    记住,GCM implementation is very easy and can be quickly implemented

    【讨论】:

      【解决方案2】:
      1. 不要在 ui-thread 中对 Web 服务进行任何请求。将您的代码移动到 Service 或 AsyncTask。前者更好。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-27
        • 2014-12-31
        • 2019-03-07
        • 1970-01-01
        • 2015-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多