-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Description
Service running with android studio working for both background and kill state but not working after bridging with react native,,,run with react native it only work when app is an background(minimize) state.
React Native version:
System:
OS: Windows 10
CPU: (4) x64 Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
Memory: 1.18 GB / 7.93 GB
Binaries:
Node: 11.11.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.15.2 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.7.0 - C:\Program Files\nodejs\npm.CMD
SDKs:
Android SDK:
API Levels: 23, 25, 26, 28
Build Tools: 28.0.3
System Images: android-26 | Google Play Intel x86 Atom, android-28 | Intel x86 Atom
IDEs:
Android Studio: Version 3.3.0.0 AI-182.5107.16.33.5314842
npmPackages:
react: 16.9.0 => 16.9.0
react-native: 0.61.2 => 0.61.2,
NetHelper class
`public class NetHelper extends ReactContextBaseJavaModule {
ReactApplicationContext reactContext;
Context context;
Intent intent;
public NetHelper(ReactApplicationContext reactContext) {
super(reactContext);
}
@OverRide
public String getName() {
return "WifiReceiver";
}
@ReactMethod
public void NetInfo() {
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
WifiReceiver wifiReceiver = new WifiReceiver();
this.reactContext.registerReceiver(wifiReceiver, filter);
}
}`
and my wifi receiver class as:
`
public class WifiReceiver extends BroadcastReceiver {
String TAG = getClass().getSimpleName();
private Context mContext;
@OverRide
public void onReceive(Context context, Intent intent) {
mContext = context;
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
networkInfo.isConnected()) {
// Wifi is connected
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
Log.e(TAG, " -- Wifi connected --- " + " SSID " + ssid );
Toast.makeText(context, " -- Wifi connected --- " + " SSID " + ssid , Toast.LENGTH_SHORT).show();
}
}
else if (intent.getAction().equalsIgnoreCase(WifiManager.WIFI_STATE_CHANGED_ACTION))
{
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
if (wifiState == WifiManager.WIFI_STATE_DISABLED)
{
Toast.makeText(context, "Status changed", Toast.LENGTH_SHORT).show();
}
}
}`