我的程序有问题,当我运行程序时,我需要 2 次迭代来更新一个名为“existe”的变量。我使用处理程序进行此更新。我认为是同步的问题,但我不知道它在哪里。我有 1 个广播接收器来接收服务方法的响应,但我认为首先它完成了按钮处理程序代码,然后它运行了广播接收器。虽然我认为广播接收器应该在接收方法服务的响应时运行?谢谢你的帮助。
这是按钮处理程序的代码:
View.OnClickListener buttonhandler=new View.OnClickListener(){
public void onClick(View v) {
EditText id_camarero = (EditText) findViewById(R.id.id_camarero);
String numero = id_camarero.getText().toString();
if( numero.trim().equals("")){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Fallo de id");
// set dialog message
alertDialogBuilder
//.setMessage("Click salir para finalizarprograma")
.setCancelable(false)
.setPositiveButton("Salir",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("Reintentar",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
idcamarero=Integer.parseInt(numero);
comprobacion();
//IntentFilter filter = new IntentFilter(MyService.DATA_RECEIVED_INTENT);
// registerReceiver(androidListener, filter);
if((existe==false)){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Fallo de id");
// set dialog message
alertDialogBuilder
//.setMessage("Click salir para finalizarprograma")
.setCancelable(false)
.setPositiveButton("Salir",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("Reintentar",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
else{
// Intent intent= new Intent(GestorRestauranteActivity.this,MenuMesas.class);
// intent.putExtra("id", numero);//enviamos el id de camarero a la actividad que invocamos
// startActivity(intent);
finish();
}
}
};
这是广播接收器的代码:
BroadcastReceiver androidListener = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(MyService.DATA_RECEIVED_INTENT))
{
Message msg = Message.obtain();
msg.obj = intent.getExtras().get("text");
msg.setTarget(cEmiHandler);
msg.sendToTarget();
}
}
};