0

サービスが接続を処理することになっている間に、読み込みプロセスを実行するためにasyタスクを使用しています。行にエラーが発生しています:

serviceIntent.putExtra("receiver",new DownloadReceiver(new Handler()));

AsyncTaskとサービスを使用して接続を行っているとき。私のコードはAsyncTask用です:

 private class DownloadReceiver extends ResultReceiver{
public DownloadReceiver(Handler handler) {
    super(handler);
}

@Override
   protected void onReceiveResult(int resultCode, Bundle resultData) {
    super.onReceiveResult(resultCode, resultData);
  //  if (resultCode == DownloadService.UPDATE_PROGRESS) {
        int progress = resultData.getInt("progress");
     //   mProgressDialog.setProgress(progress);
     //   if (progress == 100) {
       //     mProgressDialog.dismiss();
        }
    }



  protected String doInBackground(String... params) {

    Intent serviceIntent = new Intent();
            serviceIntent.putExtra("url", "www.google.com");

            new DownloadReceiver(new Handler());
            serviceIntent.putExtra("receiver",new DownloadReceiver(new Handler()));
    serviceIntent.setAction("connection.DownloadService");
    context.startService(serviceIntent);

では、何が問題になる可能性がありますか?

4

2 に答える 2

2

インテントの変更が正しいと仮定して、IntentコードをAsyncTaskのdoInBackgroundメソッドからonPostExecute関数に移動します。

doInBackground内からビューを変更することはできません。イベント・ディスパッチ・スレッドで実行されることが保証されているコールバック関数の1つで変更する必要があります。

于 2012-06-30T04:24:18.427 に答える
1
if( this error == compile time error){
`DownloadReceiver` class should implements the   Parcelable interface 
}
else{

UI以外のスレッドで実行されるメソッド内にHandlerオブジェクト を作成しているため、エラーが発生していると思いますdoInBackground()。次に、アプリケーションのどこかで、UI Threadそのオブジェクトを使用してアクセスしていHandlerます。注:これを正しく行うには、を作成する必要がありHandlerますUI Thread。あなたのケースのためにそれをするのに何と良い場所でしょう。

onPostExecute

または、インスタンス変数として配置します。そしてそれをコンストラクターに渡します。

}

于 2012-06-29T22:07:16.047 に答える