首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >progressDialog在onReceive中不会被删除

progressDialog在onReceive中不会被删除
EN

Stack Overflow用户
提问于 2013-07-22 22:32:17
回答 2查看 628关注 0票数 1

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

有没有什么理由让进展不被忽视呢?

代码语言:javascript
运行
复制
    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;
                    }

                }


        }

代码语言:javascript
运行
复制
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();
    }


}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-22 22:51:24

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

代码语言:javascript
运行
复制
 progressDialog = new ProgressDialog(NfcscannerActivity.this, themeVersion); 

你看?您可以创建新的ProgressDialog。并且你会“丢失”到上一个对话框的链接,所以它不能被忽略。将progressDialog声明为活动的字段。

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

代码语言:javascript
运行
复制
onCreate() {
    ...
    startReceiving(intent);
    progressDialog = new ProgressDialog()...
    progressDialog.show()...
}

onReceive(){
    ...
    case ERROR:
        ....
        progressDialog.dismiss();
   case FINISHED:
        ....
        progressDialog.dismiss();
}
票数 2
EN

Stack Overflow用户

发布于 2013-07-22 22:45:07

在实例化它之前,你必须在类中声明你的progressDialog:

代码语言:javascript
运行
复制
private ProgressDialog progress;

然后你可以这样做:

代码语言:javascript
运行
复制
progress = new ProgressDialog(NfcscannerActivity.this, themeVersion);
                progress.setTitle("Connecting to Server");
                progress.setMessage(" Sending data to server...");
                progress.setIndeterminate(true);

然后,在您的代码中的某处,像这样将其忽略:

代码语言:javascript
运行
复制
pogress.dismiss();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17790254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档