Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import java.io.IOException;
- public class InternetReceiver extends BroadcastReceiver {
- boolean b;
- @Override
- public void onReceive(Context context, Intent intent) {
- if (isConnected()) {
- // Internet is connected
- b = true;
- } else {
- // Internet is not connected
- b = false;
- }
- // Notify Activity accordingly
- Intent i = new Intent("broadCastName");
- // Data you need to pass to activity
- i.putExtra("message", b);
- context.sendBroadcast(i);
- }
- public boolean isConnected() {
- Runtime runtime = Runtime.getRuntime();
- try {
- Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
- int exitValue = ipProcess.waitFor();
- return (exitValue == 0);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement