2010-12-01 4 views
2

서비스가 준비되는 동안 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 방법의 말에 실행 ... 그것은 다음 활동이 시작되기 전에 바로 ... 나는 정말 손쉽게 찾을하지 않는해서 ProgressDialog를 보여주고 있다는 것입니다 이 :

// 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 메서드 내에서 무거운 작업을하고 있었기 때문에, 어떻게 든 시스템의 일부 청취자를 차단하고 BroadCastReceivers 나 Handler를 깨우지 못하게하는 것이 었습니다. onStart 코드를 병렬 스레드로 마이그레이션하면 BroadcastReceivers 및 Handlers와 함께 챔피언처럼 작동합니다. D – Pedriyoo

관련 문제