服务启动时显示 ProgressDialog

发布于 2024-10-05 02:21:20 字数 4032 浏览 1 评论 0原文

当服务准备就绪时显示 ProgressDialog 时,我遇到了严重的问题...该服务需要一些时间才能准备好,因为它有点重,所以我想在它启动的同时显示 ProgressDialog。

问题是它在下一个活动开始之前显示 ProgressDialog...我真的不知道它是什么...

package org.pfc;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class ConnectActivity extends Activity {

    // FIELDS------------------------------------------------------------------

    protected LocalService mSmeppService;
    private ProgressDialog progressDialog;

    private Thread tt;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // Gets the object to interact with the service
            mSmeppService = ((LocalService.LocalBinder) service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            // Because it is running in our same process, we should never
            // see this happen.
            mSmeppService = null;
        }
    };

    // For getting confirmation from the service
    private BroadcastReceiver serviceReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "receiver onReceive...");

            if (progressDialog.isShowing())
                progressDialog.dismiss();

            // Change activity
            Intent groupsActivityIntent = new Intent(ConnectActivity.this,
                    GroupsActivity.class);
            startActivity(groupsActivityIntent);
        }
    };

    // METHODS ----------------------------------------------------------------

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (LocalService.isRunning) {
            // TODO start ListActivity
            Log.i(TAG, "Starting GroupsScreen");

            Intent i = new Intent(ConnectActivity.this, GroupsActivity.class);
            startActivity(i);
        } else {

            setContentView(R.layout.connect_screen);

            // Add listener to the button
            Button buttonConnect = (Button) findViewById(R.id.button_connect);
            buttonConnect.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    processThread();
                }
            });
        }
    }


    // PRIVATE METHODS --------------------------------------------------------

    private void processThread() {

        progressDialog = ProgressDialog.show(ConnectActivity.this, "",
                "Loading. Please wait...", true, false);

        tt = new Thread() {
            public void run() {

                // Register broadcastReceiver to know when the service finished
                // its creation
                ConnectActivity.this.registerReceiver(serviceReceiver,
                        new IntentFilter(Intent.ACTION_VIEW));

                // Starts the service
                startService(new Intent(ConnectActivity.this,
                        LocalService.class));

                Log.i(TAG, "Receiver registered...");
            }
        };
        tt.start();
    }
}

该服务在 onStart 方法结束时执行:

// Send broadcast so activities take it
Intent i = new Intent(Intent.ACTION_VIEW);
    sendOrderedBroadcast(i, null);

因此 onReceive 方法运行,我们转到下一个活动

I'm having serious problems when showing a ProgressDialog while a service is getting ready... The service takes time to get ready as it's a bit heavy, so I want to show the ProgressDialog meanwhile it's started.

The thing is that it shows the ProgressDialog right before the next activity starts... I really don't find what it is...

package org.pfc;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class ConnectActivity extends Activity {

    // FIELDS------------------------------------------------------------------

    protected LocalService mSmeppService;
    private ProgressDialog progressDialog;

    private Thread tt;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // Gets the object to interact with the service
            mSmeppService = ((LocalService.LocalBinder) service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            // Because it is running in our same process, we should never
            // see this happen.
            mSmeppService = null;
        }
    };

    // For getting confirmation from the service
    private BroadcastReceiver serviceReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "receiver onReceive...");

            if (progressDialog.isShowing())
                progressDialog.dismiss();

            // Change activity
            Intent groupsActivityIntent = new Intent(ConnectActivity.this,
                    GroupsActivity.class);
            startActivity(groupsActivityIntent);
        }
    };

    // METHODS ----------------------------------------------------------------

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (LocalService.isRunning) {
            // TODO start ListActivity
            Log.i(TAG, "Starting GroupsScreen");

            Intent i = new Intent(ConnectActivity.this, GroupsActivity.class);
            startActivity(i);
        } else {

            setContentView(R.layout.connect_screen);

            // Add listener to the button
            Button buttonConnect = (Button) findViewById(R.id.button_connect);
            buttonConnect.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    processThread();
                }
            });
        }
    }


    // PRIVATE METHODS --------------------------------------------------------

    private void processThread() {

        progressDialog = ProgressDialog.show(ConnectActivity.this, "",
                "Loading. Please wait...", true, false);

        tt = new Thread() {
            public void run() {

                // Register broadcastReceiver to know when the service finished
                // its creation
                ConnectActivity.this.registerReceiver(serviceReceiver,
                        new IntentFilter(Intent.ACTION_VIEW));

                // Starts the service
                startService(new Intent(ConnectActivity.this,
                        LocalService.class));

                Log.i(TAG, "Receiver registered...");
            }
        };
        tt.start();
    }
}

The service executes by the end of the onStart method this:

// Send broadcast so activities take it
Intent i = new Intent(Intent.ACTION_VIEW);
    sendOrderedBroadcast(i, null);

So the onReceive method runs and we go to the next activity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情 2024-10-12 02:21:20

问题是您没有在 UI 线程中运行 ProgressDialog。

添加一个处理程序来处理 UI 线程中的消息。

private static final int UPDATE_STARTED = 0;
private static final int UPDATE_FINISHED = 1;

private Handler handler = new Handler(){
  @Override public void handleMessage(Message msg) {
    switch (msg.what) {
     case UPDATE_STARTED:
       progressDialog = ProgressDialog.show(ConnectActivity.this, "",
            "Loading. Please wait...", true, false);                
     break;  
     case UPDATE_FINISHED:
       if(progressDialog.isShowing()){
         progressDialog.dismiss();    
       }            
     break;
    }
  }
};


private void processThread() {
  Message m = new Message();
  m.what = UPDATE_STARTED;
  handler.sendMessage(m);

  //Your working code

  m = new Message();
  m.what = UPDATE_FINISHED;
  handler.sendMessage(m);
}

祝你好运!

The problem is that you are not running ProgressDialog in a UI thread.

Add a handler that will handle messages in your UI thread.

private static final int UPDATE_STARTED = 0;
private static final int UPDATE_FINISHED = 1;

private Handler handler = new Handler(){
  @Override public void handleMessage(Message msg) {
    switch (msg.what) {
     case UPDATE_STARTED:
       progressDialog = ProgressDialog.show(ConnectActivity.this, "",
            "Loading. Please wait...", true, false);                
     break;  
     case UPDATE_FINISHED:
       if(progressDialog.isShowing()){
         progressDialog.dismiss();    
       }            
     break;
    }
  }
};


private void processThread() {
  Message m = new Message();
  m.what = UPDATE_STARTED;
  handler.sendMessage(m);

  //Your working code

  m = new Message();
  m.what = UPDATE_FINISHED;
  handler.sendMessage(m);
}

good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文