Comments (6)
Hello.
Can you describe situation in which it executes twice? Does it execute twice all the time? What are steps to reproduce this problem?
It should be executed every time, when connectivity changes. For example:
- when you connect to WiFi network
- when you turn off WiFi and disconnect from WiFi network
- when you connect to mobile network
- when you disconnect from mobile network
from reactivenetwork.
Yes, the function is execute twice all the time.
Here is the activity
package com.sourcey.test;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.github.pwittchen.reactivenetwork.library.ConnectivityStatus;
import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork;
import com.sourcey.store.events.MessageEvent;
import com.sourcey.store.product.models.ProductTable;
import com.sourcey.store.user.models.LoginUser;
import com.sourcey.store.user.models.UserTable;
import com.sourcey.store.utils.Commons;
import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private static final int REQUEST_SIGNUP = 0;
private static int SPLASH_TIME_OUT = 2000;
private static Context context; // used to get the context of this activity. only use when onCreate of Activity has been called!
private LoginUser login = LoginUser.getInstance();
private ProgressDialog progressDialog;
public ReactiveNetwork reactiveNetwork;
@Bind(R.id.input_email) EditText _emailText;
@Bind(R.id.input_password) EditText _passwordText;
@Bind(R.id.btn_login) Button _loginButton;
@Bind(R.id.link_signup) TextView _signupLink;
@Bind(R.id.login_page) View login_page;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
LoginActivity.context = getApplicationContext();
setUpListeners();
}
/**
* Function used to get the application's context. Only use if the application exists!
* @return The context of this activity
*/
public static Context getAppContext() {
return LoginActivity.context;
}
public void login() {
Log.d(TAG, "Login");
if (!validate()) {
onLoginFailed();
return;
}
//Récuperation du username et du password
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
_loginButton.setEnabled(false);
progressDialog.setIndeterminate(true);
progressDialog.setMessage(getResources().getString(R.string.authenticate));
progressDialog.show();
login.requestString(Commons.URL_LOGIN, email, password);
}
public void verifyLogin() {
Log.d(TAG, "verifyLogin");
if (!validate()) {
onLoginFailed();
return;
}
//Récuperation du username et du password
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
progressDialog.setIndeterminate(true);
progressDialog.setMessage(getResources().getString(R.string.authenticate));
progressDialog.show();
if ((Commons.getPref(context, "username", "").equals(email)) && (Commons.getPref(context, "password", "").equals(password))) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
} else {
progressDialog.dismiss();
Commons.snackToast(getResources().getString(R.string.no_log), login_page, context);
_loginButton.setEnabled(true);
}
}
private int getNbUser(){
Cursor cursor = getContentResolver().query(UserTable.Contract.CONTENT_URI, null, null, null, null);
cursor.close();
return cursor.getCount();
}
private int getNbProduct(){
Cursor cursor = getContentResolver().query(ProductTable.Contract.CONTENT_URI, null, null, null, null);
cursor.close();
return cursor.getCount();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SIGNUP) {
if (resultCode == RESULT_OK) {
// If the user signs up successfully we can log them in automatically
this.finish();
}
}
}
@Override
protected void onResume() {
super.onResume();
reactiveNetwork = new ReactiveNetwork();
}
@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
protected void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
// This method will be called when a MessageEvent is posted
public void onEvent(MessageEvent event){
Toast.makeText(context, event.message, Toast.LENGTH_SHORT).show();
if(event.message==Commons.LOGIN_OK){
progressDialog.dismiss();
onLoginSuccess();
}else if(event.message==Commons.LOGIN_NOK){
progressDialog.dismiss();
_loginButton.setEnabled(true);
}
}
@Override
public void onBackPressed() {
// disable going back the the MainActivity
moveTaskToBack(true);
}
public void onLoginSuccess() {
_loginButton.setEnabled(true);
Intent j = new Intent(context, MainActivity.class);
j.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(j);
finish();
}
public void onLoginFailed() {
Toast.makeText(context, getResources().getString(R.string.no_connect), Toast.LENGTH_LONG).show();
_loginButton.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
_passwordText.setError(getResources().getString(R.string.valid_password));
valid = false;
} else {
_passwordText.setError(null);
}
if (email.isEmpty()) {
_emailText.setError(getResources().getString(R.string.valid_login));
valid = false;
} else {
_passwordText.setError(null);
}
return valid;
}
private void setUpListeners(){
progressDialog = new ProgressDialog(LoginActivity.this, R.style.AppTheme_Dark_Dialog);
_loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getNbUser() == 0) {
reactiveNetwork.observeConnectivity(context)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Action1<ConnectivityStatus>() {
@Override
public void call(ConnectivityStatus connectivityStatus) {
switch (connectivityStatus.toString()) {
case "connected to WiFi":
case "connected to mobile network":
Log.d("connect", "connected to WiFi");
login();
break;
case "offline":
Commons.snackToast(getResources().getString(R.string.no_internet), login_page, context);
break;
}
}
});
} else if (getNbUser() > 0) {
verifyLogin();
}
}
});
}
}
The function is in setUpListeners();
Thanks
from reactivenetwork.
Even if I wrote this
@Override protected void onResume() {
super.onResume();
reactiveNetwork = new ReactiveNetwork();
reactiveNetwork.observeConnectivity(this)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.filter(ConnectivityStatus.isEqualTo(ConnectivityStatus.WIFI_CONNECTED))
.subscribe(new Action1<ConnectivityStatus>() {
@Override
public void call(ConnectivityStatus connectivityStatus) {
Log.d("onResume", connectivityStatus.toString());
tvConnectivityStatus.setText(connectivityStatus.toString());
}
});
}
In the log I have twice
08-29 16:51:29.068 4486-4486/com.github.pwittchen.reactivenetwork D/onResume﹕ connected to WiFi
08-29 16:51:29.068 4486-4486/com.github.pwittchen.reactivenetwork D/onResume﹕ connected to WiFi
from reactivenetwork.
I think, you are using this library in a wrong way. You are creating subscription, when user clicks (touches) the button and then starting observing connectivity. I guess in your case, you just need to check if you have an internet connection. You can do it without any library with the following method:
public boolean isConnectedToWifiOrMobileNetwork() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
boolean isConnectedToWifiOrMobileNetwork
= networkInfo.getType() == ConnectivityManager.TYPE_WIFI
|| networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
return isConnectedToWifiOrMobileNetwork;
}
return false;
}
Nevertheless, I'll check this issue.
from reactivenetwork.
Ok Thanks
from reactivenetwork.
This problem should be fixed in PR #4. Thanks for reporting that issue.
Fix will be available in the next release.
from reactivenetwork.
Related Issues (20)
- "NetworkCallback was already unregistered" if re-using Observable HOT 4
- Migrate CI from Travis to GitHub Actions
- connection speed HOT 5
- fix error prone config for the RxJava2.x branch after bumping its version HOT 1
- Release 4.0.0-rx3 HOT 11
- [Question] What is a reason to use http instead of https protocol in your library internal requests? HOT 1
- How know which connection type is better ? HOT 1
- NetworkInfo.State Deprecated HOT 1
- Rx Java 3 support
- BroadCast Receiver stopped working HOT 5
- Google pixel wifi change event is received in 30 sec delay HOT 5
- java.lang.IllegalArgumentException: Too many NetworkRequests filed in Android Nougat(7.1.1). HOT 7
- MarshmallowNetworkObservingStrategy.java line 80 HOT 4
- isConnectedToInternet always false
- Internet connectivity issue: Could not establish connection with WalledGardenStrategy HOT 1
- ReactiveNetwork(RN) usage of IPv6 and DNS results prioritization HOT 1
- Will there be a version for Kotlin Flow ? HOT 2
- Fix Robolectric tests HOT 1
- How to check Internet Connection in Background continuously even if App is Running or Not/Closed.. HOT 1
- Th
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactivenetwork.