Android Open Source - notify Broadcast






From Project

Back to project page notify.

License

The source code is released under:

GNU General Public License

If you think the Android project notify listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.notify;
/*  w w w . ja v a2s  .co m*/
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class Broadcast implements Runnable {
    public Socket s;
    public ServerSocket ss;
    public Boolean reading = false;

    public void run() {
        try {
            if (BackgroundService.reading) {
                return;
            }
            String myip = OurUtils.getIP();
            if (myip == null) {
                return;
            }
            DatagramSocket ds = new DatagramSocket();
            ds.setBroadcast(true);
            byte[] data = ("notify " + myip).getBytes();
            DatagramPacket dg = new DatagramPacket(data, data.length, InetAddress.getByName(myip.replaceAll("\\.\\d+$", ".255")), 18082);
            ds.send(dg);
            if (!this.reading && !BackgroundService.reading) {
                this.listen();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void listen() {
        try {
            this.ss = new ServerSocket(18081);
            this.ss.setSoTimeout(4000);
            this.reading = true;
            Socket s1 = this.ss.accept();
            this.s = s1;
            OurUtils.sendMessageToActivity(EnumMessage.broadcast_response);
        } catch (SocketTimeoutException e) {
            OurUtils.sendMessageToActivity(EnumMessage.broadcast_listen_failed, "listen timed out");
        } catch (Exception e) {
            OurUtils.sendMessageToActivity(EnumMessage.broadcast_listen_failed, "unexpected error occurred");
        }
        try{
            this.ss.close();
        } catch (Exception e) {
            this.ss = null;
        }
        this.reading = false;
    }
}




Java Source Code List

com.example.notify.AccessibilityListenerService.java
com.example.notify.BackgroundService.java
com.example.notify.Broadcast.java
com.example.notify.DisplayFragments.java
com.example.notify.EnumMessage.java
com.example.notify.FragmentAccessibility.java
com.example.notify.FragmentBroadcastReconnect.java
com.example.notify.FragmentConnect.java
com.example.notify.FragmentConnected.java
com.example.notify.FragmentConnecting.java
com.example.notify.FragmentManual.java
com.example.notify.FragmentNoQrcodeScanner.java
com.example.notify.FragmentQrcodeWrong.java
com.example.notify.FragmentReconnect.java
com.example.notify.FragmentWifiWarning.java
com.example.notify.IntentBroadcastReceiver.java
com.example.notify.MainActivity.java
com.example.notify.OurUtils.java