Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. ******************************************************************************
  2. MAIN ACTIVITY FILE
  3. ******************************************************************************
  4.  
  5. package com.djag.tcpclitest2;
  6.  
  7. imports...
  8.  
  9. public class MainActivity extends Activity {
  10.  
  11. EditText edittext_Host;
  12. EditText edittext_Port;
  13. Button button_Connect;
  14. Button button_Stop;
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState)
  18. {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21.  
  22. edittext_Host = (EditText)findViewById(R.id.editText_Host);
  23. edittext_Port = (EditText)findViewById(R.id.editText_Port);
  24. button_Connect = (Button)findViewById(R.id.button_Connect);
  25. button_Stop = (Button)findViewById(R.id.button_Stop);
  26.  
  27. if (receiver != null) registerReceiver(receiver, new IntentFilter(NOTIFICATIONMAIN));
  28.  
  29. button_Connect.setOnClickListener(new View.OnClickListener()
  30. {
  31. @Override
  32. public void onClick(View view)
  33. {
  34. StartControlService();
  35. }
  36. });
  37.  
  38. button_Stop.setOnClickListener(new View.OnClickListener()
  39. {
  40. @Override
  41. public void onClick(View view)
  42. {
  43. StopControlService();
  44. }
  45. });
  46.  
  47. }
  48.  
  49. @Override
  50. protected void onDestroy()
  51. {
  52. super.onDestroy();
  53.  
  54. if (receiver != null) unregisterReceiver(receiver);
  55. }
  56.  
  57. // Start control service
  58. public void StartControlService()
  59. {
  60. Intent intent = new Intent(this, ControlService.class);
  61. //intent.putExtra(ControlService.HOST, edittext_Host.getText().toString());
  62. //intent.putExtra(ControlService.PORT, edittext_Port.getText().toString());
  63. startService(intent);
  64. }
  65.  
  66. // Request to stop control service
  67. public void StopControlService()
  68. {
  69. Intent ComIntent = new Intent(NOTIFICATIONSERVICE);
  70. ComIntent.putExtra(CONTROL, REQUESTCLOSE);
  71. ComIntent.putExtra(DATA, "");
  72. sendBroadcast(ComIntent);
  73. }
  74.  
  75. private BroadcastReceiver receiver = new BroadcastReceiver()
  76. {
  77. @Override
  78. public void onReceive(Context context, Intent intent)
  79. {
  80. Bundle bundle = intent.getExtras();
  81. if (bundle != null)
  82. {
  83. TextView textView_Status = (TextView) findViewById(R.id.textView_Status);
  84. int Control = bundle.getInt(CONTROL);
  85. String Data = bundle.getString(DATA);
  86. switch (Control)
  87. {
  88. case NOTIFYCONNECTING: ...
  89. case NOTIFYCONNECTED: ...
  90. case NOTIFYCANTCONNECT: ...
  91. case NOTIFYDISCONNECTED: ...
  92. case NOTIFYDATARECEIVED: ...
  93. case RESPONSESTATUS: ...
  94. case RESPONSEINFO: ...
  95. case LOG: ...
  96. }
  97. }
  98. }
  99. };
  100. }
  101.  
  102.  
  103. ******************************************************************************
  104. SERVICE FILE
  105. ******************************************************************************
  106.  
  107. package com.djag.tcpclitest2;
  108.  
  109. imports...
  110.  
  111. public class ControlService extends Service implements Runnable
  112. {
  113. private volatile boolean Terminate = false;
  114.  
  115. private static final int SERVICE_NOTIFICATION_ID = 1000;
  116.  
  117. @Override
  118. public IBinder onBind(Intent intent)
  119. {
  120. return null;
  121. }
  122.  
  123. @Override
  124. public void onCreate()
  125. {
  126. // Start foreground
  127. final Intent intent = new Intent(this, ControlService.class);
  128. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  129. NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  130. builder.setTicker(getString(R.string.app_name));
  131. builder.setContentTitle(getString(R.string.app_name));
  132. builder.setContentText(getString(R.string.app_name));
  133. builder.setSmallIcon(R.drawable.notification_icon);
  134. builder.setContentIntent(PendingIntent.getActivity(this, 0, intent, 0));
  135. builder.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
  136. Notification notification = builder.build();
  137. startForeground(SERVICE_NOTIFICATION_ID, notification);
  138.  
  139. }
  140.  
  141. @Override
  142. public int onStartCommand(Intent intent, int flags, int startId)
  143. {
  144. Thread t = new Thread(this);
  145. t.start();
  146.  
  147. return START_REDELIVER_INTENT;
  148. }
  149.  
  150. @Override
  151. public void onDestroy()
  152. {
  153.  
  154. }
  155.  
  156. @Override
  157. public void run()
  158. {
  159. if (receiver != null) registerReceiver(receiver, new IntentFilter(NOTIFICATIONSERVICE));
  160.  
  161. while (!Thread.currentThread().isInterrupted() && !Terminate)
  162. {
  163. synchronized (this) // not necessary
  164. {
  165.  
  166. // Here only TCP communications
  167. // Broadcast messages to the main activity using NotifyMsg
  168.  
  169. try { Thread.sleep(50); } catch (Exception e) { }
  170. }
  171. }
  172.  
  173. if (receiver != null) unregisterReceiver(receiver);
  174.  
  175. stopSelf();
  176. }
  177.  
  178. public void NotifyMsg(int Cmd, String Dat)
  179. {
  180. Intent ComIntent = new Intent(NOTIFICATIONMAIN);
  181. ComIntent.putExtra(CONTROL, Cmd);
  182. ComIntent.putExtra(DATA, Dat);
  183. sendBroadcast(ComIntent);
  184. }
  185.  
  186. // Receive broadcast messages from the main activity
  187. private BroadcastReceiver receiver = new BroadcastReceiver()
  188. {
  189. @Override
  190. public void onReceive(Context context, Intent intent)
  191. {
  192. Bundle bundle = intent.getExtras();
  193. if (bundle != null)
  194. {
  195. int Control = bundle.getInt(CONTROL);
  196. String Data = bundle.getString(DATA);
  197. switch (Control)
  198. {
  199. ...
  200. ...
  201. case REQUESTCLOSE:
  202. Terminate = true;
  203. break;
  204. }
  205. }
  206. }
  207. };
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement