我是android的新手,我想为它创建简单的应用程序。这是它应该如何工作:
observer
,因此当数据发生变化时,observable
会通知该活动,例如:
main activity
中的玩家列表需要更新connection refused
活动发送通知。之后,信息活动应显示在其他(活动)活动之上。connection refused
活动一直存在,那将是最好的,所以我可以随时显示/隐藏它问题:
connection refused
活动?应用程序中的几乎所有内容都能正常工作(连接拒绝活动会被通知连接已被拒绝)但我不知道如何将其置于最佳状态。connection refused
活动。用户不应该有可能回到被阻止的活动。ConnectionRefusedActivity:
public class ConnectionRefusedActivity extends Activity implements Observer {
private ServerService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
service = ServerService.getInstance();
service.addObserver(this);
progressDialog = new ProgressDialog(this);
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("Unable to connect to server. Click OK to reconnect.");
alertDialog.setButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
service.connect();
}
});
progressDialog.setMessage("Please wait...");
// this method tries to connect to server; if it fails `service` will sernd notification to this activity with data `false`
service.connect();
}
@Override
public void update(final Observable observable, final Object data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (observable instanceof ServerService) {
boolean isConnected = (Boolean) data;
if (isConnected) {
progressDialog.dismiss();
alertDialog.dismiss();
}
else {
// this will be called if connection with server has been refused; the problem is that I don't know how to bring this activity to top
// ATTENTION! I want to bring this activity to top here
progressDialog.show();
alertDialog.show();
}
}
}
});
}
}
connection refused activity
被通知连接已被拒绝,因此它显示信息“连接已被拒绝。您要重新连接吗?”给用户connection refused activity
答案 0 :(得分:1)
这是你可以做的,
不要让ConnectionRefusedActivity
作为观察者。你休息2活动应该是观察者。最好的方法是,保留一个实现Observer的BaseActivity。观察员通知后,请致电ConnectionRefusedActivity
打开startActivityForResult
。当用户尝试重新连接时,将结果发回以重新连接。在相应的活动中再次重新连接。