android - progressDialog 不在 onReceive 中关闭

标签 android progressdialog intentservice

我有一个向服务器发送数据的应用程序。我的 Activity 实现了 ResultReceiver.Receiver,因为它调用 IntentService,我在其中传递接收器,以便它可以将消息发送回我的 Activity 。我的 Activity 中有一个显示 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();
    }


}

最佳答案

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

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

你看到了吗?您创建新的 ProgressDialog。并且您“丢失”了与之前对话的链接,因此无法将其关闭。 将您的 progressDialog 声明为您的 Activity 字段。

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

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

onReceive(){
    ...
    case ERROR:
        ....
        progressDialog.dismiss();
   case FINISHED:
        ....
        progressDialog.dismiss();
}

关于android - progressDialog 不在 onReceive 中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17790254/

相关文章:

具有线程问题的Android ProgressDialog

android - 进度对话框 - WindowManager$BadTokenException : Unable to add window -- token null is not for an application

android - 如何在运行时将两个 fragment 添加到 frameLayout 中

java - 如何将 OnClickListener 从 Activity 传递到 Fragment

android - android中的空白屏幕加载器

android - 如何取消Service/IntentService/AsyncTask/Looper

Android:当应用程序被杀死时保持服务运行

android - 意向服务问题

android - 为什么 android 在 looper 等待时处理 ANR 中的应用程序

php - Android Php Mysql 插入重复