我正在開發一個應用程序,並且我被封鎖在一件簡單的事情中。android兩次調用相同的對話框
在我的活動,我表明問一個郵件地址和激活對話框(AlertDialog.Builder)。這兩個字段用Rest API檢查。
如果激活碼是錯了,我重新開始活動(有意向),我再次顯示該對話框。
我不明白爲什麼,如果我錯了激活碼的第一次,第二次正確出現的對話框中,但是當我點擊「提交」,應用程序不運行REST調用和返回總是「無效憑證」,就像它會提醒舊的「狀態」一樣。
相反,如果我運行應用程序,我把正確的憑據,一切正常。
有什麼想法?
源代碼:
public class PinActivity extends Activity {
String mail;
String verification;
JSONObject responseServer;
BluetoothSocket bsocket;
ConnectedThreadBT cdbt;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
EditText mail_add;
EditText verification_code;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check);
setup();
dialogActivation();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void setup(){
RestClientManager.initialize(getApplicationContext()).enableDebugLog(true);
bsocket = BluetoothApplication.getBSocket();
//salvo codice attivazione sul pacchetto
cdbt=new ConnectedThreadBT(bsocket,mHandler, "PinActivity");
cdbt.start();
}
private void dialogActivation(){
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_dialog_verification, null);
mail_add = (EditText) view.findViewById(R.id.mailAddress);
verification_code = (EditText) view.findViewById(R.id.verification_code);
builder.setView(view).
setPositiveButton(getApplicationContext().getResources().getString(R.string.submit), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//prendo e salvo credenziali
mail = mail_add.getText().toString();
verification = verification_code.getText().toString();
//invio dati al server
activatePPS();
}
});
builder.setCancelable(false);
builder.show();
}
private void activatePPS(){
dialogCheck();
String url = "....";
RestClientManager.getInstance().makeJsonRequest(Request.Method.POST, url, new RequestHandler<>(new RequestCallbacks<JSONObject, Error>()
{
@Override
public void onRequestSuccess(JSONObject response)
{
responseServer = response;
int reply_code = 0;
try {
reply_code = response.getInt("reply_code");
checkReplyCode(reply_code);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onRequestError(Error error)
{
}
}, paramsList()));
}
private void dialogCheck(){
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_dialog_load_check, null);
builder.setView(view);
builder.setCancelable(false);
builder.show();
}
private void checkReplyCode(int reply_code) throws JSONException, IOException {
switch(reply_code){
case 0:
successActivation();
break;
case 1001:
//credenziali invalide
Toast.makeText(getApplicationContext(), getResources().getString(R.string.wrong_credentials), Toast.LENGTH_LONG).show();
Intent intent = new Intent(PinActivity.this, PinActivity.class);
startActivity(intent);
break;
}
}
private void successActivation() throws JSONException {
String access_token = responseServer.get("access_token").toString();
String nickname = responseServer.get(".....
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int value = sharedPref.getInt("step_conf",0);
if(value==0){
Intent intent = new Intent(getApplicationContext(), MethodCurveActivity.class);
intent.putExtra("style", 0);
startActivity(intent);
}
else{
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
},3000);
}
private ArrayMap<String, String> paramsList(){
ArrayMap<String, String> parameters=new ArrayMap<>();
parameters.put("user_mail", mail);
parameters.put(.....
return parameters;
}
private void resetMobileDevice(){
String url = "....";
RestClientManager.getInstance().makeJsonRequest(Request.Method.POST, url, new RequestHandler<>(new RequestCallbacks<JSONObject, Error>()
{
@Override
public void onRequestSuccess(JSONObject response)
{
System.out.println("Risposta:"+response);
responseServer = response;
int reply_code = 0;
try {
reply_code = response.getInt("reply_code");
} catch (JSONException e) {
e.printStackTrace();
}
try {
checkReplyCode(reply_code);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onRequestError(Error error)
{
}
}, paramsList()));
}
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
}
}
};
}
重要的一點是在 「情況1001」,錯誤之後。 我試圖完成()和所有要刪除的活動的舊實例的方法......
很難建議沒有您的源代碼的東西。 – eleven
@eleven這裏的源代碼 – hteo
爲什麼你要重新啓動錯誤憑證的活動,爲什麼不重新彈出對話框? – AndroidGeek