1

我有一个将数据发送到服务器的应用程序。我的活动实现了 ResultReceiver.Receiver,因为它调用了一个 IntentService,我将接收器传入其中,以便它可以将消息发送回我的活动。我的活动中有一个显示progressDialog 的onReceive 方法。它显示正确,但是当服务完成并发送回 FINISHED 消息时,progressDialog 不会关闭。

进度不被驳回是有原因的吗?

    MyActivity extends Activity implements ResultReceiver.Receiver{

    onCreate(){

    mReceiver = new MyResultReceiver(new Handler());
            mReceiver.setReceiver(this);


final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, QueryService.class);
        intent.putExtra("receiver", mReceiver);
        intent.putExtra("command", "query");

        intent.putExtra("compid", compID);
        intent.putExtra("tagid", tagId);
        intent.putExtra("tagclientid", tagClientId);
        intent.putExtra("carerid", carerID);
        intent.putExtra("formattedtagscantime", formattedTagScanTime);
        intent.putExtra("formattednowtime", formattedNowTime);
        intent.putExtra("statusforwebservice", statusForWbService);
        intent.putExtra("devicename", getDeviceName() + getDeviceName());
        intent.putExtra("taglatitude", tagLatitude);
        intent.putExtra("taglongitude", tagLongitude);

        startService(intent);

    }

        ......
        .....


        @Override
                public void onReceiveResult(int resultCode, Bundle resultData) {


                    final int RUNNING = 1;
                    final int FINISHED = 2;
                    final int ERROR = 3;



                    int buildVersionSdk = Build.VERSION.SDK_INT;
                    int buildVersionCodes = Build.VERSION_CODES.GINGERBREAD;

                    Log.e(TAG, "buildVersionSdk = " + buildVersionSdk 
                            + "buildVersionCodes = " + buildVersionCodes);

                    int themeVersion;
                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) {

                         themeVersion = 2;

                    }else{

                         themeVersion = 1;
                    }

                    progressDialog = new ProgressDialog(NfcscannerActivity.this, themeVersion);
                    progressDialog.setTitle("Connecting to Server");
                    progressDialog.setMessage(" Sending data to server...");
                    progressDialog.setIndeterminate(true);

                     switch (resultCode) {
                        case RUNNING:
                            //show progress
                            Log.e(TAG, "queryService running");


                            try{
                            progressDialog.show();
                            }catch(Exception e){

                                //ignore
                            }
                            break;
                        case FINISHED:

                            progressDialog.dismiss();

                            String result = resultData.getString("result");

                            Log.e(TAG, "result in onreceive from posting service = " + result);


                            if( result != null && result.trim().equalsIgnoreCase("OK")  ){

                                Log.e(TAG, "about to update DB with servertime");
                                DateTime sentToServerAt = new DateTime();
                                nfcscannerapplication.loginValidate.updateTransactionWithServerTime(sentToServerAt,null);


                                tagId = null;
                                tagType = null;
                                tagClientId = null;

                                //called to refresh the unsent transactions textview
                                onResume();

                            }else if(result != null && result.trim().equalsIgnoreCase("Error: TX duplicated")){
                                Log.e(TAG, "response from server is Duplicate Transaction ");


                                //NB. the following time may not correspond exactly with the time on the server
                                //because this TX has already been processed but the 'OK' never reached the phone,
                                //so we are just going to update the phone's DB with the DupTX time so the phone doesn't keep 
                                //sending it.

                                DateTime sentToServerTimeWhenDupTX = new DateTime();
                                nfcscannerapplication.loginValidate.updateTransactionWithServerTime(sentToServerTimeWhenDupTX,null);

                                tagId = null;
                                tagType = null;
                                tagClientId = null;



                            }else{

                                Toast.makeText(NfcscannerActivity.this,
                                        "No phone signal or server problem",
                                        Toast.LENGTH_LONG).show();
                            }



                            break;
                        case ERROR:
                            // handle the error;
                            progressDialog.dismiss();
                            break;
                    }

                }


        }

.

public class QueryService extends IntentService {

    final int STATUS_RUNNING = 1;
    final int STATUS_FINISHED = 2;
    final int STATUS_ERROR = 3;
    private static final String TAG = QueryService.class.getSimpleName();
    NfcScannerApplication nfcscannerapplication;

    public QueryService() {
        super("QueryService");



    }

    protected void onHandleIntent(Intent intent) {

        Log.e(TAG, "inside onHandleIntent in QueryService");

        nfcscannerapplication = (NfcScannerApplication)getApplication();

        final ResultReceiver receiver = intent.getParcelableExtra("receiver");
        String command = intent.getStringExtra("command");
        Bundle b = new Bundle();
        if(command.equals("query")) {
            receiver.send(STATUS_RUNNING, Bundle.EMPTY);
            try {
                // get some data or something    




                String compID = intent.getStringExtra("compid");
                String tagID = intent.getStringExtra("tagid");
                String tagClientID = intent.getStringExtra("tagclientid");
                String carerID = intent.getStringExtra("carerid");
                String formattedTagScanTime = intent.getStringExtra("formattedtagscantime");
                String formattedNowTime = intent.getStringExtra("formattednowtime");
                String statusForWebService = intent.getStringExtra("statusforwebservice");
                String deviceName = intent.getStringExtra("devicename");
                String tagLatitude = intent.getStringExtra("taglatitude");
                String tagLongitude = intent.getStringExtra("taglongitude");

                Log.e(TAG, "params in queryservice = " + compID + tagID + tagClientID + carerID + formattedTagScanTime +
                        formattedNowTime + statusForWebService + deviceName + tagLatitude + tagLongitude);


                String result = nfcscannerapplication.loginWebservice.postData(compID, tagID, tagClientID, carerID, formattedTagScanTime,
                        formattedNowTime, statusForWebService, deviceName, tagLatitude, tagLongitude);

                Log.e(TAG, "RESULT FROM POST IN QUERYSERVICE = " + result);

                b.putString("result", result);

               // b.putParcelableArrayList("results", results);
                receiver.send(STATUS_FINISHED, b);
            } catch(Exception e) {
                b.putString(Intent.EXTRA_TEXT, e.toString());
                receiver.send(STATUS_ERROR, b);
            }    
        }
        this.stopSelf();
    }


}
4

2 回答 2

2

您不能关闭进度对话框,因为每次应用程序进入 onReceive() 方法时都会创建一个新的进度对话框。

 progressDialog = new ProgressDialog(NfcscannerActivity.this, themeVersion); 

你看?您创建新的 ProgressDialog。而且您“丢失”了指向上一个对话框的链接,因此无法将其关闭。将您的 progressDialog 声明为您的活动字段。

例如,如果您根本不想更改代码(但我建议添加 UPLOAD_STARTED 案例):

onCreate() {
    ...
    startReceiving(intent);
    progressDialog = new ProgressDialog()...
    progressDialog.show()...
}

onReceive(){
    ...
    case ERROR:
        ....
        progressDialog.dismiss();
   case FINISHED:
        ....
        progressDialog.dismiss();
}
于 2013-07-22T14:51:24.393 回答
0

在像这样实例化它之前,您必须在 Class 中声明您的progressDialog:

private ProgressDialog progress;

那么你可以这样做:

progress = new ProgressDialog(NfcscannerActivity.this, themeVersion);
                progress.setTitle("Connecting to Server");
                progress.setMessage(" Sending data to server...");
                progress.setIndeterminate(true);

然后在您的代码中的某个地方像这样将其关闭:

pogress.dismiss();
于 2013-07-22T14:45:07.143 回答