我有一个将数据发送到服务器的应用程序。我的活动实现了ResultReceiver.Receiver,因为它调用了一个IntentService,我在其中传递接收器,以便它可以将消息发送回我的活动。我的活动中有一个onReceive方法,显示progressDialog。它显示正确,但是当服务完成并发回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();
}
}
答案 0 :(得分:2)
您无法关闭进度对话框,因为每次您的应用进入onReceive()方法时都会创建一个新的进度对话框。
progressDialog = new ProgressDialog(NfcscannerActivity.this, themeVersion);
你知道吗?您创建NEW ProgressDialog。并且你“丢失”链接到你以前的对话框,所以它不能被解雇。
将progressDialog声明为您活动的字段。
例如,如果您根本不想更改代码(但我建议添加UPLOAD_STARTED案例):
onCreate() {
...
startReceiving(intent);
progressDialog = new ProgressDialog()...
progressDialog.show()...
}
onReceive(){
...
case ERROR:
....
progressDialog.dismiss();
case FINISHED:
....
progressDialog.dismiss();
}
答案 1 :(得分:0)
你必须在类实例化它之前在类中声明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();