Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ******************************************************************************
- MAIN ACTIVITY FILE
- ******************************************************************************
- package com.djag.tcpclitest2;
- imports...
- public class MainActivity extends Activity {
- EditText edittext_Host;
- EditText edittext_Port;
- Button button_Connect;
- Button button_Stop;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- edittext_Host = (EditText)findViewById(R.id.editText_Host);
- edittext_Port = (EditText)findViewById(R.id.editText_Port);
- button_Connect = (Button)findViewById(R.id.button_Connect);
- button_Stop = (Button)findViewById(R.id.button_Stop);
- if (receiver != null) registerReceiver(receiver, new IntentFilter(NOTIFICATIONMAIN));
- button_Connect.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View view)
- {
- StartControlService();
- }
- });
- button_Stop.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View view)
- {
- StopControlService();
- }
- });
- }
- @Override
- protected void onDestroy()
- {
- super.onDestroy();
- if (receiver != null) unregisterReceiver(receiver);
- }
- // Start control service
- public void StartControlService()
- {
- Intent intent = new Intent(this, ControlService.class);
- //intent.putExtra(ControlService.HOST, edittext_Host.getText().toString());
- //intent.putExtra(ControlService.PORT, edittext_Port.getText().toString());
- startService(intent);
- }
- // Request to stop control service
- public void StopControlService()
- {
- Intent ComIntent = new Intent(NOTIFICATIONSERVICE);
- ComIntent.putExtra(CONTROL, REQUESTCLOSE);
- ComIntent.putExtra(DATA, "");
- sendBroadcast(ComIntent);
- }
- private BroadcastReceiver receiver = new BroadcastReceiver()
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Bundle bundle = intent.getExtras();
- if (bundle != null)
- {
- TextView textView_Status = (TextView) findViewById(R.id.textView_Status);
- int Control = bundle.getInt(CONTROL);
- String Data = bundle.getString(DATA);
- switch (Control)
- {
- case NOTIFYCONNECTING: ...
- case NOTIFYCONNECTED: ...
- case NOTIFYCANTCONNECT: ...
- case NOTIFYDISCONNECTED: ...
- case NOTIFYDATARECEIVED: ...
- case RESPONSESTATUS: ...
- case RESPONSEINFO: ...
- case LOG: ...
- }
- }
- }
- };
- }
- ******************************************************************************
- SERVICE FILE
- ******************************************************************************
- package com.djag.tcpclitest2;
- imports...
- public class ControlService extends Service implements Runnable
- {
- private volatile boolean Terminate = false;
- private static final int SERVICE_NOTIFICATION_ID = 1000;
- @Override
- public IBinder onBind(Intent intent)
- {
- return null;
- }
- @Override
- public void onCreate()
- {
- // Start foreground
- final Intent intent = new Intent(this, ControlService.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
- builder.setTicker(getString(R.string.app_name));
- builder.setContentTitle(getString(R.string.app_name));
- builder.setContentText(getString(R.string.app_name));
- builder.setSmallIcon(R.drawable.notification_icon);
- builder.setContentIntent(PendingIntent.getActivity(this, 0, intent, 0));
- builder.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
- Notification notification = builder.build();
- startForeground(SERVICE_NOTIFICATION_ID, notification);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId)
- {
- Thread t = new Thread(this);
- t.start();
- return START_REDELIVER_INTENT;
- }
- @Override
- public void onDestroy()
- {
- }
- @Override
- public void run()
- {
- if (receiver != null) registerReceiver(receiver, new IntentFilter(NOTIFICATIONSERVICE));
- while (!Thread.currentThread().isInterrupted() && !Terminate)
- {
- synchronized (this) // not necessary
- {
- // Here only TCP communications
- // Broadcast messages to the main activity using NotifyMsg
- try { Thread.sleep(50); } catch (Exception e) { }
- }
- }
- if (receiver != null) unregisterReceiver(receiver);
- stopSelf();
- }
- public void NotifyMsg(int Cmd, String Dat)
- {
- Intent ComIntent = new Intent(NOTIFICATIONMAIN);
- ComIntent.putExtra(CONTROL, Cmd);
- ComIntent.putExtra(DATA, Dat);
- sendBroadcast(ComIntent);
- }
- // Receive broadcast messages from the main activity
- private BroadcastReceiver receiver = new BroadcastReceiver()
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Bundle bundle = intent.getExtras();
- if (bundle != null)
- {
- int Control = bundle.getInt(CONTROL);
- String Data = bundle.getString(DATA);
- switch (Control)
- {
- ...
- ...
- case REQUESTCLOSE:
- Terminate = true;
- break;
- }
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement