1

AsyncTaskがRemoteServiceにバインドしているときに、無期限のProgressDialogを表示しようとしています。RemoteServiceは、サービスが最初に作成されたときにユーザーの連絡先のリストを作成します。連絡先の長いリストの場合、これには5〜10秒かかる場合があります。

私が抱えている問題は、RemoteServiceが連絡先のリストを作成するまでProgressDialogが表示されないことです。ProgressDialogが表示される時間を与えるために、Thread.sleepを入れてみました。sleepステートメントを使用すると、ProgressDialogがロードされて回転を開始しますが、RemoteServiceが動作を開始するとすぐにロックアップします。

AsyncTaskをダミーコードに変えて、しばらくスリープ状態にすると、すべてが正常に機能します。しかし、タスクが実際の作業を行う必要がある場合、それはUIがただ座って待機しているようなものです。

私が間違っていることについてのアイデアはありますか?

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(IM,"Start Me UP!!");
    setContentView(R.layout.main);
    Log.d(IM, "Building List View for Contacts");
    restoreMe();
     if (myContacts==null){
         myContacts = new ArrayList<Contact>();
         this.contactAdapter = new ContactAdapter(this, 
                                                  R.layout.contactlist, 
                                                  myContacts);
         setListAdapter(this.contactAdapter);
         new BindAsync().execute();
     }
     else{
         this.contactAdapter = new ContactAdapter(this, 
                                                  R.layout.contactlist, 
                                                  myContacts);
         setListAdapter(this.contactAdapter);

     }
}

private class BindAsync extends AsyncTask<Void, Void, RemoteServiceConnection>{
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        Log.d(IM,"Showing Dialog");
        showDialog(DIALOG_CONTACTS);
    }
    @Override
    protected RemoteServiceConnection doInBackground(Void... v) {
        Log.d(IM,"Binding to service in BindAsync");
        try{
        Thread.sleep(2000);
        } catch (InterruptedException e){

        }

        RemoteServiceConnection myCon;
        myCon = new RemoteServiceConnection();
        Intent i = new Intent(imandroid.this,MyRemoteService.class);
       bindService(i, myCon, Context.BIND_AUTO_CREATE);
        startService(i);
        Log.d(IM,"Bound to remote service");
        return myCon;
    }
    @Override
    protected void onPostExecute(RemoteServiceConnection newConn){
        super.onPostExecute(newConn);
        Log.d(IM,"Storing remote connection");
        conn=newConn;
    }

};

編集:onCreateDialogを追加

 protected Dialog onCreateDialog(int id){
    switch(id){
    case DIALOG_CONTACTS:
        ProgressDialog progDialog = new ProgressDialog(imandroid.this);
        progDialog.setMessage("Loading Contacts... Please Wait");
        progDialog.setCancelable(false);
        return progDialog;
    default:
        return super.onCreateDialog(id);
    }
}
4

1 に答える 1

0

bindService()from を実行しないでくださいdoInBackground()。まず、これはほぼ瞬時に実行されるため、バックグラウンド スレッドで実行する必要はありません。つまり、CPU 時間とバッテリーを浪費するだけです。Looper第二に、それはあなたのメッセージキューと連携する必要があるContextため、バックグラウンドスレッドに置くことは私見では危険です。

また、サービスへのバインドとサービスの開始の両方を行っていることに注意してください。それが適切な場合もいくつかありますが、通常はどちらか一方だけが必要です。

于 2010-04-24T11:40:04.040 に答える