Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4.  
  5. import java.io.IOException;
  6.  
  7. public class InternetReceiver extends BroadcastReceiver {
  8. boolean b;
  9.  
  10. @Override
  11. public void onReceive(Context context, Intent intent) {
  12.  
  13. if (isConnected()) {
  14. // Internet is connected
  15. b = true;
  16. } else {
  17. // Internet is not connected
  18. b = false;
  19. }
  20.  
  21. // Notify Activity accordingly
  22. Intent i = new Intent("broadCastName");
  23. // Data you need to pass to activity
  24. i.putExtra("message", b);
  25.  
  26. context.sendBroadcast(i);
  27. }
  28.  
  29. public boolean isConnected() {
  30.  
  31. Runtime runtime = Runtime.getRuntime();
  32. try {
  33.  
  34. Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
  35. int exitValue = ipProcess.waitFor();
  36. return (exitValue == 0);
  37.  
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. } catch (InterruptedException e) {
  41. e.printStackTrace();
  42. }
  43.  
  44. return false;
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement