Skip to content

Commit e4a9f67

Browse files
committed
Some changes in UI
and background thread is included
1 parent 63e4864 commit e4a9f67

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
android:label="@string/app_name"
1212
android:supportsRtl="true"
1313
android:theme="@style/AppTheme.NoActionBar">
14-
<service
15-
android:name=".Services.SendInBackground"
16-
android:exported="false"/>
17-
<activity android:name=".MainActivity">
14+
<activity android:name=".MainActivity"
15+
android:screenOrientation="portrait">
1816
<intent-filter>
1917
<action android:name="android.intent.action.MAIN" />
2018

app/src/main/java/com/example/rajatjain/multicasttestapp/fragment/Reciever.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.net.Uri;
55
import android.net.wifi.WifiManager;
6+
import android.os.AsyncTask;
67
import android.os.Bundle;
78
import android.os.StrictMode;
89
import android.support.v4.app.Fragment;
@@ -39,8 +40,7 @@ public void onCreate(Bundle savedInstanceState) {
3940
@Override
4041
public View onCreateView(LayoutInflater inflater, ViewGroup container,
4142
Bundle savedInstanceState) {
42-
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
43-
StrictMode.setThreadPolicy(policy);
43+
4444
// Inflate the layout for this fragment
4545
view= inflater.inflate(R.layout.fragment_reciever, container, false);
4646

@@ -68,44 +68,63 @@ public void onDetach() {
6868
@Override
6969
public void onClick(View view) {
7070

71-
Context c = view.getContext();
72-
takeWifi(c, true);
71+
Log.e("clicked","clicked");
72+
73+
final View temp=view;
74+
AsyncTask.execute(new Runnable() {
75+
@Override
76+
public void run() {
77+
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
78+
7379

74-
String mip = mIpAddress.getText().toString();
75-
String port = mPort.getText().toString();
76-
InetAddress group = null;
77-
MulticastSocket socket = null;
7880

7981
try {
82+
Context c = temp.getContext();
83+
takeWifi(c, true);
84+
String mip = mIpAddress.getText().toString();
85+
String port = mPort.getText().toString();
86+
InetAddress group = null;
87+
MulticastSocket socket = null;
8088
group = InetAddress.getByName(mip);
8189
socket = new MulticastSocket(Integer.parseInt(port));
8290
socket.joinGroup(group);
91+
8392
while (true) {
8493
byte[] buf = new byte[1000];
8594
DatagramPacket recv = new DatagramPacket(buf, buf.length);
8695
Log.d("VIVZ", "Datagram packet created");
8796
socket.receive(recv);
8897
Log.d("VIVZ", "It was recvd");
8998
String received = new String(recv.getData(), 0, recv.getLength());
90-
if (received == "STOP") {
91-
updatePC();
92-
updateUI(received);
99+
if (received.equals("STOP")) {
100+
/* updatePC();
101+
updateUI(received);*/
93102
break;
94103

95104
}
96105

97106
//Initially just for simplicity
98-
updatePC();
99-
updateUI(received);
100-
}
107+
final String tempString=received;
108+
getActivity().runOnUiThread(new Runnable() {
109+
@Override
110+
public void run() {
111+
updatePC();
112+
updateUI(tempString);
113+
}
114+
});
115+
116+
}
101117
takeWifi(c, false);
102118
} catch (Exception e) {
103119
e.printStackTrace();
104120
Log.d("VIVZ", String.valueOf(e));
105121
}
106122

107-
123+
return;
124+
}
125+
});
108126
}
127+
109128
public void updateUI(String recvd) {
110129

111130
packetstatus.setText("Packet Received: " + packetcount);

0 commit comments

Comments
 (0)