I want to check Internet connection throughout the run time of my Android application. I tried using services but seems like it is not the best option. Is there any possible way for me to implement a broadcast receiver with in a service? Or do I have to give up the service and use broadcast receivers alone?
            Asked
            
        
        
            Active
            
        
            Viewed 9,857 times
        
    7
            
            
        - 
                    Why the connection check in Service? Why not on each http/ https request? – Pankaj Kumar May 06 '13 at 08:47
 - 
                    Thanks for the comment @Pankaj Kumar; actually it is online streaming, so need to keep checking the connection every second – DbxD May 06 '13 at 09:26
 
3 Answers
9
            I will show you how to create SMS receiver in a service:
public class MyService extends Service {
@Override
public void onCreate() {
    BwlLog.begin(TAG);
    super.onCreate();
    SMSreceiver mSmsReceiver = new SMSreceiver();
    IntentFilter filter = new IntentFilter();
    filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
    filter.addAction(SMS_RECEIVE_ACTION); // SMS
    filter.addAction(WAP_PUSH_RECEIVED_ACTION); // MMS
    this.registerReceiver(mSmsReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}
   /**
 * This class used to monitor SMS
 */
class SMSreceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (TextUtils.equals(intent.getAction(), SMS_RECEIVE_ACTION)) {
             //handle sms receive
        }
    }
}
- 
                    2
 - 
                    2
 - 
                    OK, I know I'm very late to ask here, but what if I need to send the result from onReceive back to the service? I've been struggling to solve this issue, with no luck! – Lamar Nov 28 '18 at 22:42
 - 
                    I have an activity which sends an SMS. Do I have to use `sendBroadcast()` or `startService()` to call the service class, which also includes the broadcast receiver as you've mentioned in your code? – May 11 '21 at 02:11
 
3
            
            
        It wouldn't be wise to check for the connectivity every second. Alternatively you can listen to the action (ConnectivityManager.CONNECTIVITY_ACTION) and identify if you are connected to an active network or not.
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
Additionally you can check the network Type that is currently active(Type_WIFI, Type_MOBILE)
This way, you don't need a service that keeps checking the connectivity every second.
        siddharthsn
        
- 1,709
 - 1
 - 21
 - 32
 
- 
                    Think this would help. It will be more helpful if you can refer me to some example code. Thank you – DbxD May 06 '13 at 11:30
 
1
            
            
        You need not to use Service or BroadCastReceiver for this purpose. Just check Connection status everyTime you need to ping the server.
you can write a method which checks this and returns a boolean(true/false) according to connection status.
Below method does the same.
public static boolean isNetworkAvailable(Context mContext) {
        try {
            final ConnectivityManager conn_manager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo network_info = conn_manager
                    .getActiveNetworkInfo();
            if (network_info != null && network_info.isConnected()) {
                if (network_info.getType() == ConnectivityManager.TYPE_WIFI)
                    return true;
                else if (network_info.getType() == ConnectivityManager.TYPE_MOBILE)
                    return true;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;
    }
        AAnkit
        
- 27,299
 - 12
 - 60
 - 71
 
- 
                    Thanks for the comment @AndroidEnthusiast; in my code i always have to use internet connection, actually it is online streaming, so need to keep check the connection every second – DbxD May 06 '13 at 09:05