2010-12-01 60 views
2

当服务准备就绪时显示ProgressDialog时,我遇到了严重的问题...服务需要时间做好准备,因为它有点重,所以我想显示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方法运行,我们去到下一个活动

回答

3

的问题是,你是不是在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); 
} 

好运!

+0

感谢您的询问。我尝试过,但没有奏效。我最终发现了这个问题。问题是我在onStart方法(在Service中)做了大量工作,所以工作阻塞了某些系统的监听器,BroadCastReceivers和Handlers都没有被唤醒。一旦我将onStart代码迁移到一个并行线程,它就像一个冠军一样,都使用BroadcastReceivers和Handlers:D – Pedriyoo 2010-12-07 10:20:55