想要这样做:我想在应用程序的开始时检查互联网,如果它没有找到,那么点击“正面”按钮就可以进入设置wifi&如果用户在wifi上然后回到App我想要关闭对话框并且动画启动,否则它再次显示互联网对话框。
我做了什么:我在OnResume()中放置了Internet Check Dialog,在OnCreate中放置了动画代码。
问题是:在我的应用程序启动时,它检查Wifi连接,但它也在连续运行Oncreate()中的所有动画代码,而不是仅在Internet连接后运行它
OnCreate代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.grow_from_middle, R.anim.shrink_to_middle);
setContentView(R.layout.activity_csplogin);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
mobileEdit = (EditText) findViewById(R.id.mobileText);
nameEdit = (EditText) findViewById(R.id.nameText);
employerEdit = (EditText) findViewById(R.id.employerText);
noEmployerCheckbox = (CheckBox) findViewById(R.id.noEmployercheckboxid);
employerSpinner = (Spinner) findViewById(R.id.employer_spinner_id);
noEmployerLayout = (LinearLayout) findViewById(R.id.linearlayoutCheckbox);
init();
if (myPrefs.getOrgValidated() == false) {
new OrganisationValidationTask(CSPLoginActivity.this).execute();
}
isdeviceValidated = myPrefs.getIsDeviceValidated();
isLoggedIn = myPrefs.getIsLogIn();
if (isdeviceValidated) {
startLoginActivity();
}
final RelativeLayout LoginBox = (RelativeLayout) findViewById(R.id.LoginBox);
LoginBox.setVisibility(View.GONE);
Animation animTranslate = AnimationUtils.loadAnimation(CSPLoginActivity.this, R.anim.translate);
animTranslate.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
LoginBox.setVisibility(View.VISIBLE);
Animation animFade = AnimationUtils.loadAnimation(CSPLoginActivity.this, R.anim.fade);
LoginBox.startAnimation(animFade);
showSingleChoice();
}
});
ImageView imgLogo = (ImageView) findViewById(R.id.imageView1);
imgLogo.startAnimation(animTranslate);
isdeviceValidated = myPrefs.getIsDeviceValidated();
isLoggedIn = myPrefs.getIsLogIn();
if (!isLoggedIn) {
// display login screen
if (Utils.isNetworkConnected(this)) {
if (isdeviceValidated) {
// to display user details
// displayUserDetails();
if (!isMyServiceRunning()) {
Utils.startLocationPollerAndWakeupService(this);
}
}
}
} else if (isLoggedIn && isdeviceValidated) {
// skip login screen
if (!isMyServiceRunning()) {
Utils.startLocationPollerAndWakeupService(this);
}
startLoginActivity();
}
}
OnResume
@Override
protected void onResume() {
super.onResume();
if(Utils.isNetworkConnected(this)) {
}else{
showWifiAlert();
}
}
检查“Internet连接”对话框:
private void showWifiAlert(){
new MaterialDialog.Builder(CSPLoginActivity.this)
.content("Unable to validate device as Internet not available")
.title("Alert !")
.positiveText("OK")
.negativeText("Cancel")
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onNegative(MaterialDialog dialog) {
finish();
}
@Override
public void onPositive(MaterialDialog dialog) {
dialog.dismiss();
startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
}
})
.cancelable(false)
.show();
}
请帮助,如何实现我想做的事。
答案 0 :(得分:1)
尝试替换onResume方法代码,如下所示:
@Override
protected void onResume() {
super.onResume();
if(Utils.isNetworkConnected(this)) {
imgLogo.startAnimation(animTranslate);
}else{
showWifiAlert();
}
}
请告诉我这是否适合您。