I have a service class that I want to use to check for internet both when app is closed and in the background. If app is online a method will be called from within service class This is the code for the service
public class MyService extends Service {
public MyService() {
}
NotificationCompat.Builder builder;
private FirebaseAuth.AuthStateListener mAuthListener;
static final String CONNECTIVITY_CHANGE_ACTION="android.net.conn.CONNECTIVITY_CHANGE";
@Nullable
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
// throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
Toast.makeText(getApplicationContext(), "checking for new bookings", Toast.LENGTH_SHORT).show();
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (CONNECTIVITY_CHANGE_ACTION.equals(action)) {
if (!ConnectionHelper.isConnectedOrConnecting(context)) {
if (context != null) {
boolean show = false;
if (ConnectionHelper.lastNoConnectionTs == -1) {
show = true;
ConnectionHelper.lastNoConnectionTs = System.currentTimeMillis();
} else {
if (System.currentTimeMillis() - ConnectionHelper.lastNoConnectionTs > 1000) {
show = true;
ConnectionHelper.lastNoConnectionTs = System.currentTimeMillis();
}
}
if (show && ConnectionHelper.isOnline) {
ConnectionHelper.isOnline = false;
Log.i("NETWORK123", "Connection lost");
}
}
} else {
Log.i("NETWORK123", "Connection lost");
ConnectionHelper.isOnline = true;
Methodwhenappisonline();
}
}
}
};
registerReceiver(receiver,filter);
return START_STICKY;
}
@Override
public void onDestroy(){
super.onDestroy();
Toast.makeText(getApplicationContext(),"service killed",Toast.LENGTH_SHORT).show();
}
}
Below is my connection helper class
public class ConnectionHelper {
public static long lastNoConnectionTs=-1;
public static boolean isOnline=true;
public static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnected();
}
public static boolean isConnectedOrConnecting(Context context){
ConnectivityManager cm=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork=cm.getActiveNetworkInfo();
return activeNetwork!=null&&activeNetwork.isConnectedOrConnecting();
// activeNetwork.isConnectedOrConnecting();
}
}
And below is my manifest
<service
android:name=". MyService"
android:enabled="true"
android:exported=true></service>
The service is to run wehh the app is in background and when it is closed. However when the app is in the background it is destroyed after some time and when it is closed it is destroyed instantly. Please help. Please note my minimum sdk version is 16.