Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mainActivity
- ===========================================
- package com.example.zahra.lightpolice;
- import android.app.Activity;
- 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;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity {
- Activity activity;
- BoundedService boundedService;
- Button start_btn, stop_btn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- activity = this;
- start_btn = (Button) findViewById(R.id.start);
- stop_btn = (Button) findViewById(R.id.stop);
- }
- ServiceConnection serviceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- //cast the IBinder and get service instance
- MyBinder binder = ((MyBinder) service);
- boundedService = binder.myBinder;
- boundedService.policeLight(activity);
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- boundedService.isOn = false;
- }
- };
- // strat & bind to Bounded Service
- public void startPoliceLight(View view) {
- super.onStart();
- Intent myIntent = new Intent(this, BoundedService.class);
- bindService(myIntent, serviceConnection, Context.BIND_AUTO_CREATE);
- }
- // stop & unbind service
- public void stopPoliceLight(View view) {
- boundedService.isOn = false;
- unbindService(serviceConnection);
- super.onStop();
- }
- }
- =====================================================
- myBinder
- =====================================================
- package com.example.zahra.lightpolice;
- import android.os.Binder;
- /**
- * Created by zahra on 11/12/2017.
- */
- public class MyBinder extends Binder {
- public BoundedService myBinder ;
- }
- ======================================================
- service
- ======================================================
- package com.example.zahra.lightpolice;
- import android.app.Activity;
- import android.app.Service;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.IBinder;
- import android.support.annotation.Nullable;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- /**
- * Created by zahra on 11/12/2017.
- */
- public class BoundedService extends Service{
- Activity activity;
- LinearLayout redColor, blueColor;
- Runnable light1, light2, light3;
- final int SLEEP = 300;
- protected Boolean isOn = false;
- @Nullable
- @Override
- public IBinder onBind(Intent intent) { //Using methods of IBinder Class
- Toast.makeText(this, "Service Was bounded", Toast.LENGTH_SHORT).show();
- MyBinder binder = new MyBinder();
- binder.myBinder = this;
- isOn = true;
- return binder;
- }
- @Override
- public boolean onUnbind(Intent intent) {
- Toast.makeText(this, " unbound service", Toast.LENGTH_LONG).show();
- return super.onUnbind(intent);
- }
- public void policeLight(final Activity activity) {
- this.activity = activity;
- redColor = activity.findViewById(R.id.red_layout);
- blueColor = activity.findViewById(R.id.blue_Layout);
- light1 = new Runnable() {
- @Override
- public void run() {
- redColor.setBackgroundColor(Color.RED);
- blueColor.setBackgroundColor(Color.BLUE);
- }
- };
- light2 = new Runnable() {
- @Override
- public void run() {
- redColor.setBackgroundColor(Color.BLUE);
- blueColor.setBackgroundColor(Color.RED);
- }
- };
- light3 = new Runnable() {
- @Override
- public void run() {
- redColor.setBackgroundColor(Color.WHITE);
- blueColor.setBackgroundColor(Color.WHITE);
- }
- };
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (isOn) {
- try {
- activity.runOnUiThread(light1);
- Thread.sleep(SLEEP);
- activity.runOnUiThread(light2);
- Thread.sleep(SLEEP);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- }
- =======================================================
- xml
- =======================================================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <LinearLayout
- android:layout_width="123dp"
- android:layout_height="match_parent"
- android:layout_marginBottom="400dp"
- android:orientation="vertical">
- <Button
- android:id="@+id/stop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="stopPoliceLight"
- android:text="Stop" />
- <Button
- android:id="@+id/start"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="startPoliceLight"
- android:text="Start" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/red_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginTop="100dp"
- android:layout_weight="1"
- android:orientation="horizontal"></LinearLayout>
- <LinearLayout
- android:id="@+id/blue_Layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginTop="100dp"
- android:layout_weight="1"
- android:orientation="horizontal">
- </LinearLayout>
- </LinearLayout>
- ======================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement