если мое приложение запускается в автономном режиме и после того, как я включил данные Wi-Fi / мобильный, мой диалог прогресса должен скрыться и показать снэк-бар без перезапуска приложения.
Я следовал этому примеру: https://www.androidhive.info/2012/07/android-detect-internet-connection-status/
Но когда я включаю или выключаю интернет, он не дает немедленного эффекта без повторного запуска приложения.
Код:
package com.jimmytrivedi.lapitchat.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FacebookAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.jimmytrivedi.lapitchat.Application.LapitChat;
import com.jimmytrivedi.lapitchat.Interface.ConnectivityReceiverListener;
import com.jimmytrivedi.lapitchat.R;
import com.jimmytrivedi.lapitchat.Receiver.ConnectivityReceiver;
import butterknife.BindView;
import butterknife.ButterKnife;
public class StartActivity extends AppCompatActivity implements View.OnClickListener, ConnectivityReceiverListener {
private static final String TAG = StartActivity.class.getName();
@BindView(R.id.signup)
Button signup;
@BindView(R.id.login)
Button login;
@BindView(R.id.phoneAuth)
Button phoneAuth;
@BindView(R.id.facebook_button)
LoginButton facebookButton;
@BindView(R.id.relative_layout)
RelativeLayout relativeLayout;
private CallbackManager mCallbackManager;
private FirebaseAuth mAuth;
private ProgressDialog progressDialog;
private FirebaseUser currentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
ButterKnife.bind(this);
init();
checkConnection();
setClickListener();
clickOnFacebook();
}
private void clickOnFacebook() {
mCallbackManager = CallbackManager.Factory.create();
facebookButton.setReadPermissions("email", "public_profile");
facebookButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "Facebook:onSuccess: " + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Log.e(TAG, "facebook:onCancel");
}
@Override
public void onError(FacebookException error) {
Log.e(TAG, "facebook:onError", error);
}
});
}
private void setClickListener() {
signup.setOnClickListener(this);
login.setOnClickListener(this);
phoneAuth.setOnClickListener(this);
}
public void init() {
progressDialog = new ProgressDialog(this);
mAuth = FirebaseAuth.getInstance();
}
private boolean checkConnection() {
boolean isConnected = ConnectivityReceiver.isConnected();
showNetworkStatus(isConnected);
return true;
}
@Override
public void onStart() {
super.onStart();
currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
updateUI();
}
}
private void updateUI() {
startActivity(new Intent(StartActivity.this, MainActivity.class));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result back to the Facebook SDK
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = mAuth.getCurrentUser();
updateUI();
} else {
Toast.makeText(StartActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View view) {
int id = view.getId();
Intent intent;
switch (id) {
case R.id.signup:
intent = new Intent(StartActivity.this, RegisterActivity.class);
startActivity(intent);
break;
case R.id.login:
intent = new Intent(StartActivity.this, LoginActivity.class);
startActivity(intent);
break;
case R.id.phoneAuth:
intent = new Intent(StartActivity.this, PhoneAuthActivity.class);
startActivity(intent);
break;
}
}
private void showNetworkStatus(boolean isConnected) {
String message = "";
int color = 0;
if (isConnected) {
progressDialog.dismiss();
message = "Connected to Internet";
color = Color.WHITE;
Snackbar snackbar = Snackbar
.make(relativeLayout, message, Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
TextView textView = sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(color);
snackbar.show();
} else {
progressDialog.setTitle(getResources().getString(R.string.check_connection));
progressDialog.setMessage(getResources().getString(R.string.wait));
progressDialog.setCancelable(false);
progressDialog.show();
}
}
@Override
protected void onResume() {
super.onResume();
LapitChat.getInstance().setConnectivityListener(this);
}
@Override
public void onNetworkConnectionChanged(boolean isConnected) {
showNetworkStatus(isConnected);
}
}