在服务类中显示ProgressDialog

在服务类中显示ProgressDialog

本文介绍了在服务类中显示ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用服务使用AsyncTask连接到网络.我想显示一个ProgressDialog,直到该应用程序连接到网络为止.但是我该怎么办呢?

I am using a Service to connect to a network using a AsyncTask. I want to show a ProgressDialog till the app is connected to the network. But how can I do this?

我的服务如下:

package de.bertrandt.bertrandtknx;

import tuwien.auto.calimero.link.KNXNetworkLinkIP;
import tuwien.auto.calimero.process.ProcessCommunicator;
import de.bertrandt.bertrandtknx.Controls.OnOff;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.IBinder;
import android.widget.Toast;


public class ConnectionService  extends Service {

    public static KNXNetworkLinkIP m_netLinkIp = null;
    private static ProcessCommunicator m_pc = null;


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
       //code to execute when the service is first created
        new Connect().execute();
    }

    @Override
    public void onDestroy() {
       //code to execute when the service is shutting down
        new Disconnect().execute();
    }

    public void onStartCommand(Intent intent, int startid) {
       //code to execute when the service is starting up
    }


    /**
     * GetIPAddress Async
     * */
    public String getIpAddr() {
           WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
           WifiInfo wifiInfo = wifiManager.getConnectionInfo();
           int ip = wifiInfo.getIpAddress();

           String ipString = String.format(
           "%d.%d.%d.%d",
           (ip & 0xff),
           (ip >> 8 & 0xff),
           (ip >> 16 & 0xff),
           (ip >> 24 & 0xff));

           return ipString.toString();
    }

    /**
     * Connect Async
     * */
    private class Connect extends AsyncTask<String, Void, String> {
          ProgressDialog dialog;
          boolean ok;
          @Override
          protected String doInBackground(String... params) {
              try {
                  //get local IP address
                  String ipAddress = getIpAddr();
                  System.out.println("WiFi address is " + ipAddress);

                  m_netLinkIp = Calimero.Util.connect(ipAddress, "192.168.0.2");

                  if (m_netLinkIp == null){

                      System.out.println("Can not connect to Demobard");
                      ok = false;
                  }
                  else{
                      System.out.println("Connected to Demoboard");
                      ok = true;

                      }
               } catch (Exception e) {
                   e.printStackTrace();
               }
               return null;
          }

          @Override
          protected void onPostExecute(String result) {
            //dialog.dismiss();
            Toast.makeText(getApplicationContext(),
                    "Verbindung mit Demoboard " +
                            ((ok == true) ? "hergestellt" : "fehlgeschlagen"), Toast.LENGTH_LONG).show();
            if(ok == false){
                //show reconnect dialog
                //reconnect_dialog();
            }
          }

          @Override
          protected void onPreExecute() {
           // Setup Progress Dialog
           dialog = new ProgressDialog(OnOff.this);
           dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
           dialog.setMessage("Bitte warten, verbinde mit KNX-Board");
           dialog.setIndeterminate(true);
           dialog.show();*/
          }
    }

    /**
     * Disconnect Async
     * */
    private class Disconnect extends AsyncTask<String, Void, String> {
          @Override
          protected String doInBackground(String... params) {
                    try {
                        Calimero.Util.disconnect(m_netLinkIp);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                return null;
          }

    }

}

当然,此代码会出现问题,如何获得启动服务的活动的上下文?

Of course this code makes problems how can I get the context of the activity which starts the Service?

对话框应显示在启动服务的活动中,直到连接应用为止.

The Dialog should be shown in the activity which starts the Service until the app is connected.

推荐答案

因此,服务不是用户界面,您不必使用观察者模式.您的活动必须在服务中注册一个侦听器,以便该服务可以向活动通知特殊事件(例如开始加载或完成加载).

So a service is NOT the UI you have to use the observer pattern. Your activity have to register a listener in the service so that the service can inform the activity for special events (like start loading or finish loading).

您必须在服务类别中添加以下界面:

You have to add following intercae into your service class:

    public interface serviceObserver {
        public void startLoading();
        public void stopLoading();
    }

您的活动必须实现serviceObserver.您的服务必须存储来自serviceObserver的实例,该实例是在活动中创建的.如果您的服务在没有活动的情况下运行,我建议您使用广播接收器进行通信.

Your activity has to implement serviceObserver. Your service has to store a instance from serviceObserver which is created in the activity.If your service is running without your activity i recommend to use broadcast receiver for communication.

这篇关于在服务类中显示ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:29