Advertisement
m_naish

Services - Zahraa

Nov 20th, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. mainActivity
  2. ===========================================
  3.  
  4. package com.example.zahra.lightpolice;
  5.  
  6. import android.app.Activity;
  7. import android.content.ComponentName;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.ServiceConnection;
  11. import android.os.Bundle;
  12. import android.os.IBinder;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.view.View;
  15. import android.widget.Button;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.     Activity activity;
  19.     BoundedService boundedService;
  20.     Button start_btn, stop_btn;
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.         setPointer();
  27.     }
  28.  
  29.     private void setPointer() {
  30.         activity = this;
  31.         start_btn = (Button) findViewById(R.id.start);
  32.         stop_btn = (Button) findViewById(R.id.stop);
  33.  
  34.     }
  35.  
  36.     ServiceConnection serviceConnection = new ServiceConnection() {
  37.         @Override
  38.         public void onServiceConnected(ComponentName name, IBinder service) {
  39.             //cast the IBinder and get service instance
  40.             MyBinder binder = ((MyBinder) service);
  41.             boundedService = binder.myBinder;
  42.             boundedService.policeLight(activity);
  43.  
  44.         }
  45.  
  46.         @Override
  47.         public void onServiceDisconnected(ComponentName name) {
  48.             boundedService.isOn = false;
  49.         }
  50.     };
  51.  
  52.     // strat & bind to Bounded Service
  53.     public void startPoliceLight(View view) {
  54.         super.onStart();
  55.         Intent myIntent = new Intent(this, BoundedService.class);
  56.         bindService(myIntent, serviceConnection, Context.BIND_AUTO_CREATE);
  57.  
  58.     }
  59.  
  60.     // stop & unbind service
  61.     public void stopPoliceLight(View view) {
  62.         boundedService.isOn = false;
  63.         unbindService(serviceConnection);
  64.         super.onStop();
  65.  
  66.     }
  67.  
  68. }
  69. =====================================================
  70. myBinder
  71. =====================================================
  72. package com.example.zahra.lightpolice;
  73.  
  74. import android.os.Binder;
  75.  
  76. /**
  77.  * Created by zahra on 11/12/2017.
  78.  */
  79.  
  80. public class MyBinder extends Binder {
  81.  
  82.   public BoundedService myBinder ;
  83. }
  84.  
  85. ======================================================
  86. service
  87. ======================================================
  88. package com.example.zahra.lightpolice;
  89.  
  90. import android.app.Activity;
  91. import android.app.Service;
  92. import android.content.Intent;
  93. import android.graphics.Color;
  94. import android.os.IBinder;
  95. import android.support.annotation.Nullable;
  96. import android.widget.LinearLayout;
  97. import android.widget.Toast;
  98.  
  99. /**
  100.  * Created by zahra on 11/12/2017.
  101.  */
  102.  
  103. public class BoundedService extends Service{
  104.     Activity activity;
  105.     LinearLayout redColor, blueColor;
  106.     Runnable light1, light2, light3;
  107.     final int SLEEP = 300;
  108.     protected Boolean isOn = false;
  109.  
  110.  
  111.     @Nullable
  112.     @Override
  113.     public IBinder onBind(Intent intent) { //Using methods of IBinder Class
  114.         Toast.makeText(this, "Service Was bounded", Toast.LENGTH_SHORT).show();
  115.         MyBinder binder = new MyBinder();
  116.         binder.myBinder = this;
  117.         isOn = true;
  118.         return binder;
  119.     }
  120.  
  121.     @Override
  122.     public boolean onUnbind(Intent intent) {
  123.         Toast.makeText(this, " unbound service", Toast.LENGTH_LONG).show();
  124.         return super.onUnbind(intent);
  125.  
  126.     }
  127.  
  128.     public void policeLight(final Activity activity) {
  129.         this.activity = activity;
  130.         redColor = activity.findViewById(R.id.red_layout);
  131.         blueColor = activity.findViewById(R.id.blue_Layout);
  132.  
  133.  
  134.         light1 = new Runnable() {
  135.             @Override
  136.             public void run() {
  137.                 redColor.setBackgroundColor(Color.RED);
  138.                 blueColor.setBackgroundColor(Color.BLUE);
  139.             }
  140.         };
  141.         light2 = new Runnable() {
  142.             @Override
  143.             public void run() {
  144.                 redColor.setBackgroundColor(Color.BLUE);
  145.                 blueColor.setBackgroundColor(Color.RED);
  146.             }
  147.         };
  148.         light3 = new Runnable() {
  149.             @Override
  150.             public void run() {
  151.                 redColor.setBackgroundColor(Color.WHITE);
  152.                 blueColor.setBackgroundColor(Color.WHITE);
  153.             }
  154.         };
  155.  
  156.         new Thread(new Runnable() {
  157.             @Override
  158.             public void run() {
  159.                 while (isOn) {
  160.  
  161.                     try {
  162.                         activity.runOnUiThread(light1);
  163.                         Thread.sleep(SLEEP);
  164.                         activity.runOnUiThread(light2);
  165.                         Thread.sleep(SLEEP);
  166.                     } catch (InterruptedException e) {
  167.                         e.printStackTrace();
  168.                     }
  169.                 }
  170.             }
  171.         }).start();
  172.  
  173.     }
  174. }
  175. =======================================================
  176. xml
  177. =======================================================
  178. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  179.     android:layout_width="match_parent"
  180.     android:layout_height="match_parent"
  181.     android:orientation="horizontal">
  182.  
  183.     <LinearLayout
  184.         android:layout_width="123dp"
  185.         android:layout_height="match_parent"
  186.         android:layout_marginBottom="400dp"
  187.         android:orientation="vertical">
  188.  
  189.         <Button
  190.             android:id="@+id/stop"
  191.             android:layout_width="wrap_content"
  192.             android:layout_height="wrap_content"
  193.             android:onClick="stopPoliceLight"
  194.             android:text="Stop" />
  195.  
  196.         <Button
  197.             android:id="@+id/start"
  198.             android:layout_width="wrap_content"
  199.             android:layout_height="wrap_content"
  200.             android:onClick="startPoliceLight"
  201.             android:text="Start" />
  202.     </LinearLayout>
  203.     <LinearLayout
  204.         android:id="@+id/red_layout"
  205.         android:layout_width="match_parent"
  206.         android:layout_height="match_parent"
  207.         android:layout_marginTop="100dp"
  208.         android:layout_weight="1"
  209.         android:orientation="horizontal"></LinearLayout>
  210.  
  211.     <LinearLayout
  212.         android:id="@+id/blue_Layout"
  213.         android:layout_width="match_parent"
  214.         android:layout_height="match_parent"
  215.         android:layout_marginTop="100dp"
  216.         android:layout_weight="1"
  217.         android:orientation="horizontal">
  218.  
  219.     </LinearLayout>
  220. </LinearLayout>
  221.  
  222. ======================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement