mmayoub

Chakala, hw on Bound Service

Nov 13th, 2017
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. activity_main.xml
  2. -----------------
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:id="@+id/llyBack"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical">
  8.  
  9.     <TextView
  10.         android:id="@+id/tvLight1"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="match_parent"
  13.         android:layout_weight="1"
  14.         android:gravity="center"
  15.         android:textSize="50sp" />
  16.  
  17.     <TextView
  18.         android:id="@+id/tvLight2"
  19.         android:layout_width="match_parent"
  20.         android:layout_height="match_parent"
  21.         android:layout_weight="1"
  22.         android:gravity="center"
  23.         android:textSize="50sp" />
  24. </LinearLayout>
  25.  
  26. MainActivity.java
  27. -----------------
  28. package com.example.mohamadpc.hckruboundservice;
  29.  
  30. import android.content.ComponentName;
  31. import android.content.Context;
  32. import android.content.Intent;
  33. import android.content.ServiceConnection;
  34. import android.os.Bundle;
  35. import android.os.IBinder;
  36. import android.support.v7.app.AppCompatActivity;
  37. import android.view.View;
  38.  
  39. public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
  40.     private MyService myService;
  41.     private boolean isBound = false;
  42.  
  43.     @Override
  44.     protected void onCreate(Bundle savedInstanceState) {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.activity_main);
  47.  
  48.         setPointer();
  49.     }
  50.  
  51.     private void setPointer() {
  52.  
  53.         findViewById(R.id.llyBack).setOnClickListener(this);
  54.         findViewById(R.id.llyBack).setOnLongClickListener(this);
  55.     }
  56.  
  57.     @Override
  58.     public void onClick(View v) {
  59.         if (myService.getActivity() == null) {
  60.             myService.setActivity(this, findViewById(R.id.tvLight1), findViewById(R.id.tvLight2));
  61.         } else {
  62.             myService.setNoActivity();
  63.         }
  64.     }
  65.  
  66.     @Override
  67.     protected void onStart() {
  68.         super.onStart();
  69.         bind();
  70.     }
  71.  
  72.     private void bind() {
  73.         // Bind to LocalService
  74.         Intent intent = new Intent(this, MyService.class);
  75.         bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
  76.     }
  77.  
  78.     /**
  79.      * Defines callbacks for service binding, passed to bindService()
  80.      */
  81.     private ServiceConnection myConnection = new ServiceConnection() {
  82.  
  83.         @Override
  84.         public void onServiceConnected(ComponentName className,
  85.                                        IBinder service) {
  86.             // We've bound to MyService, cast the IBinder and get MyService instance
  87.             MyService.MyBinder myBinder = (MyService.MyBinder) service;
  88.             myService = myBinder.getService();
  89.             isBound = true;
  90.         }
  91.  
  92.         @Override
  93.         public void onServiceDisconnected(ComponentName arg0) {
  94.             isBound = false;
  95.         }
  96.     };
  97.  
  98.     @Override
  99.     protected void onStop() {
  100.         super.onStop();
  101.         myService.setNoActivity();
  102.     }
  103.  
  104.     @Override
  105.     public boolean onLongClick(View v) {
  106.         myService.changeMode();
  107.         return true;
  108.     }
  109. }
  110.  
  111. MyService.java
  112. --------------
  113. package com.example.mohamadpc.hckruboundservice;
  114.  
  115.  
  116. import android.app.Activity;
  117. import android.app.Service;
  118. import android.content.Intent;
  119. import android.graphics.Color;
  120. import android.os.Binder;
  121. import android.os.IBinder;
  122. import android.support.annotation.Nullable;
  123. import android.util.Log;
  124. import android.view.View;
  125.  
  126. /**
  127.  * Created by MOHAMADPC on 12/11/2017.
  128.  */
  129.  
  130. public class MyService extends Service {
  131.     private final IBinder myBinder = new MyBinder();
  132.     private int state = 0;
  133.     private Activity myActivity = null;
  134.     private View light1, light2;
  135.     private State[] myStates;
  136.     private int mode = 0;
  137.  
  138.  
  139.     public MyService() {
  140.         myStates = setPolice();
  141.  
  142.         new Thread(new Runnable() {
  143.             @Override
  144.             public void run() {
  145.  
  146.                 while (true) {
  147.                     state = (state + 1) % myStates.length;
  148.                     Log.d("TESTING", "state=" + state);
  149.                     if (myActivity != null) {
  150.  
  151.                         myActivity.runOnUiThread(new Runnable() {
  152.  
  153.                             @Override
  154.                             public void run() {
  155.                                 light1.setBackgroundColor(myStates[state].color1);
  156.                                 light2.setBackgroundColor(myStates[state].color2);
  157.                             }
  158.                         });
  159.                     }
  160.                     try {
  161.                         Thread.sleep(myStates[state].delay);
  162.                     } catch (InterruptedException e) {
  163.                         e.printStackTrace();
  164.                     }
  165.                 }
  166.             }
  167.         }).start();
  168.     }
  169.  
  170.     public State[] setPolice() {
  171.         State[] police = new State[12];
  172.         police[0] = new State(Color.RED, Color.BLACK, 120);
  173.         police[1] = new State(Color.BLACK, Color.BLACK, 50);
  174.         police[2] = new State(Color.RED, Color.BLACK, 120);
  175.         police[3] = new State(Color.BLACK, Color.BLACK, 50);
  176.         police[4] = new State(Color.RED, Color.BLACK, 120);
  177.         police[5] = new State(Color.BLACK, Color.BLACK, 70);
  178.  
  179.         police[6] = new State(Color.BLACK, Color.BLUE, 120);
  180.         police[7] = new State(Color.BLACK, Color.BLACK, 50);
  181.         police[8] = new State(Color.BLACK, Color.BLUE, 120);
  182.         police[9] = new State(Color.BLACK, Color.BLACK, 50);
  183.         police[10] = new State(Color.BLACK, Color.BLUE, 120);
  184.         police[11] = new State(Color.BLACK, Color.BLACK, 70);
  185.  
  186.         return police;
  187.     }
  188.  
  189.     public State[] setPolice1() {
  190.         State[] police = new State[12];
  191.         police[0] = new State(Color.BLUE, Color.BLACK, 120);
  192.         police[1] = new State(Color.BLACK, Color.BLACK, 50);
  193.         police[2] = new State(Color.BLUE, Color.BLACK, 120);
  194.         police[3] = new State(Color.BLACK, Color.BLACK, 50);
  195.         police[4] = new State(Color.BLUE, Color.BLACK, 120);
  196.         police[5] = new State(Color.BLACK, Color.BLACK, 70);
  197.  
  198.         police[6] = new State(Color.BLACK, Color.BLUE, 120);
  199.         police[7] = new State(Color.BLACK, Color.BLACK, 50);
  200.         police[8] = new State(Color.BLACK, Color.BLUE, 120);
  201.         police[9] = new State(Color.BLACK, Color.BLACK, 50);
  202.         police[10] = new State(Color.BLACK, Color.BLUE, 120);
  203.         police[11] = new State(Color.BLACK, Color.BLACK, 70);
  204.  
  205.         return police;
  206.     }
  207.  
  208.     public State[] setFire() {
  209.         State[] fire = new State[12];
  210.         fire[0] = new State(Color.RED, Color.BLACK, 120);
  211.         fire[1] = new State(Color.BLACK, Color.BLACK, 50);
  212.         fire[2] = new State(Color.RED, Color.BLACK, 120);
  213.         fire[3] = new State(Color.BLACK, Color.BLACK, 50);
  214.         fire[4] = new State(Color.RED, Color.BLACK, 120);
  215.         fire[5] = new State(Color.BLACK, Color.BLACK, 70);
  216.  
  217.         fire[6] = new State(Color.BLACK, Color.RED, 120);
  218.         fire[7] = new State(Color.BLACK, Color.BLACK, 50);
  219.         fire[8] = new State(Color.BLACK, Color.RED, 120);
  220.         fire[9] = new State(Color.BLACK, Color.BLACK, 50);
  221.         fire[10] = new State(Color.BLACK, Color.RED, 120);
  222.         fire[11] = new State(Color.BLACK, Color.BLACK, 70);
  223.  
  224.         return fire;
  225.     }
  226.  
  227.     @Nullable
  228.     @Override
  229.  
  230.     public IBinder onBind(Intent intent) {
  231.         return myBinder;
  232.     }
  233.  
  234.  
  235.     public class MyBinder extends Binder {
  236.         public MyService getService() {
  237.             return MyService.this;
  238.         }
  239.  
  240.     }
  241.  
  242.     public Activity getActivity() {
  243.         return myActivity;
  244.     }
  245.  
  246.     public void setActivity(Activity activity, View light1, View light2) {
  247.         this.myActivity = activity;
  248.         this.light1 = light1;
  249.         this.light2 = light2;
  250.     }
  251.  
  252.     public void setNoActivity() {
  253.         this.myActivity = null;
  254.         this.light1 = null;
  255.         this.light2 = null;
  256.     }
  257.  
  258.     private class State {
  259.         int color1;
  260.         int color2;
  261.         int delay;
  262.  
  263.         public State(int color1, int color2, int delay) {
  264.             this.color1 = color1;
  265.             this.color2 = color2;
  266.             this.delay = delay;
  267.         }
  268.     }
  269.  
  270.     public void changeMode() {
  271.         mode = (mode + 1) % 3;
  272.         switch (mode) {
  273.             case 0:
  274.                 myStates = setPolice();
  275.                 break;
  276.             case 1:
  277.                 myStates = setFire();
  278.                 break;
  279.             case 2:
  280.                 myStates = setPolice1();
  281.                 break;
  282.         }
  283.     }
  284. }
RAW Paste Data