Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity_main.xml
- -----------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/llyBack"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tvLight1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:textSize="50sp" />
- <TextView
- android:id="@+id/tvLight2"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:textSize="50sp" />
- </LinearLayout>
- MainActivity.java
- -----------------
- package com.example.mohamadpc.hckruboundservice;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
- private MyService myService;
- private boolean isBound = false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- findViewById(R.id.llyBack).setOnClickListener(this);
- findViewById(R.id.llyBack).setOnLongClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if (myService.getActivity() == null) {
- myService.setActivity(this, findViewById(R.id.tvLight1), findViewById(R.id.tvLight2));
- } else {
- myService.setNoActivity();
- }
- }
- @Override
- protected void onStart() {
- super.onStart();
- bind();
- }
- private void bind() {
- // Bind to LocalService
- Intent intent = new Intent(this, MyService.class);
- bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
- }
- /**
- * Defines callbacks for service binding, passed to bindService()
- */
- private ServiceConnection myConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName className,
- IBinder service) {
- // We've bound to MyService, cast the IBinder and get MyService instance
- MyService.MyBinder myBinder = (MyService.MyBinder) service;
- myService = myBinder.getService();
- isBound = true;
- }
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- isBound = false;
- }
- };
- @Override
- protected void onStop() {
- super.onStop();
- myService.setNoActivity();
- }
- @Override
- public boolean onLongClick(View v) {
- myService.changeMode();
- return true;
- }
- }
- MyService.java
- --------------
- package com.example.mohamadpc.hckruboundservice;
- import android.app.Activity;
- import android.app.Service;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Binder;
- import android.os.IBinder;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import android.view.View;
- /**
- * Created by MOHAMADPC on 12/11/2017.
- */
- public class MyService extends Service {
- private final IBinder myBinder = new MyBinder();
- private int state = 0;
- private Activity myActivity = null;
- private View light1, light2;
- private State[] myStates;
- private int mode = 0;
- public MyService() {
- myStates = setPolice();
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- state = (state + 1) % myStates.length;
- Log.d("TESTING", "state=" + state);
- if (myActivity != null) {
- myActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- light1.setBackgroundColor(myStates[state].color1);
- light2.setBackgroundColor(myStates[state].color2);
- }
- });
- }
- try {
- Thread.sleep(myStates[state].delay);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- public State[] setPolice() {
- State[] police = new State[12];
- police[0] = new State(Color.RED, Color.BLACK, 120);
- police[1] = new State(Color.BLACK, Color.BLACK, 50);
- police[2] = new State(Color.RED, Color.BLACK, 120);
- police[3] = new State(Color.BLACK, Color.BLACK, 50);
- police[4] = new State(Color.RED, Color.BLACK, 120);
- police[5] = new State(Color.BLACK, Color.BLACK, 70);
- police[6] = new State(Color.BLACK, Color.BLUE, 120);
- police[7] = new State(Color.BLACK, Color.BLACK, 50);
- police[8] = new State(Color.BLACK, Color.BLUE, 120);
- police[9] = new State(Color.BLACK, Color.BLACK, 50);
- police[10] = new State(Color.BLACK, Color.BLUE, 120);
- police[11] = new State(Color.BLACK, Color.BLACK, 70);
- return police;
- }
- public State[] setPolice1() {
- State[] police = new State[12];
- police[0] = new State(Color.BLUE, Color.BLACK, 120);
- police[1] = new State(Color.BLACK, Color.BLACK, 50);
- police[2] = new State(Color.BLUE, Color.BLACK, 120);
- police[3] = new State(Color.BLACK, Color.BLACK, 50);
- police[4] = new State(Color.BLUE, Color.BLACK, 120);
- police[5] = new State(Color.BLACK, Color.BLACK, 70);
- police[6] = new State(Color.BLACK, Color.BLUE, 120);
- police[7] = new State(Color.BLACK, Color.BLACK, 50);
- police[8] = new State(Color.BLACK, Color.BLUE, 120);
- police[9] = new State(Color.BLACK, Color.BLACK, 50);
- police[10] = new State(Color.BLACK, Color.BLUE, 120);
- police[11] = new State(Color.BLACK, Color.BLACK, 70);
- return police;
- }
- public State[] setFire() {
- State[] fire = new State[12];
- fire[0] = new State(Color.RED, Color.BLACK, 120);
- fire[1] = new State(Color.BLACK, Color.BLACK, 50);
- fire[2] = new State(Color.RED, Color.BLACK, 120);
- fire[3] = new State(Color.BLACK, Color.BLACK, 50);
- fire[4] = new State(Color.RED, Color.BLACK, 120);
- fire[5] = new State(Color.BLACK, Color.BLACK, 70);
- fire[6] = new State(Color.BLACK, Color.RED, 120);
- fire[7] = new State(Color.BLACK, Color.BLACK, 50);
- fire[8] = new State(Color.BLACK, Color.RED, 120);
- fire[9] = new State(Color.BLACK, Color.BLACK, 50);
- fire[10] = new State(Color.BLACK, Color.RED, 120);
- fire[11] = new State(Color.BLACK, Color.BLACK, 70);
- return fire;
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return myBinder;
- }
- public class MyBinder extends Binder {
- public MyService getService() {
- return MyService.this;
- }
- }
- public Activity getActivity() {
- return myActivity;
- }
- public void setActivity(Activity activity, View light1, View light2) {
- this.myActivity = activity;
- this.light1 = light1;
- this.light2 = light2;
- }
- public void setNoActivity() {
- this.myActivity = null;
- this.light1 = null;
- this.light2 = null;
- }
- private class State {
- int color1;
- int color2;
- int delay;
- public State(int color1, int color2, int delay) {
- this.color1 = color1;
- this.color2 = color2;
- this.delay = delay;
- }
- }
- public void changeMode() {
- mode = (mode + 1) % 3;
- switch (mode) {
- case 0:
- myStates = setPolice();
- break;
- case 1:
- myStates = setFire();
- break;
- case 2:
- myStates = setPolice1();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement