Show ProgressDialog in class Service

I am using the service to connect to the network using AsyncTask. I want to show ProgressDialog until the application is connected to the network. But how can I do this?

My service looks like 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 creates problems, how can I get the activity context that starts the Service?

The dialog should be displayed in the action that launches the Service until the application is connected.

+3
android android-asynctask android-service progressdialog
source share
1 answer

Therefore, the service is NOT a user interface; you must use the observer pattern. Your activity should register a listener with the service so that the service can inform you about activities for special events (for example, start a download or complete a download).

You must add the following intercae to your class of service:

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

Your activity should implement serviceObserver. Your service must store an instance of serviceObserver that is created in this action. If your service is running without your activity, I recommend using a broadcast receiver for communication.

+7
source share

All Articles