Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity
- ==================
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.IBinder;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity {
- Activity activity; // set activity
- BoundedService boundedService; // set BoundedService class
- Button button,off_btn; // set buttons
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- activity = this;
- button = (Button) findViewById(R.id.on_button);
- off_btn = (Button)findViewById(R.id.off_btn);
- }
- // this method to start the onBind method and start the service
- public void startMe(View view)
- {
- Intent myIntent = new Intent(this, BoundedService.class);
- bindService(myIntent, serviceConnection, Context.BIND_AUTO_CREATE);
- }
- // This method to start the onUnbind and stop the Service
- public void stopMe(View view)
- {
- boundedService.isRun = false;
- unbindService(serviceConnection);
- }
- ServiceConnection serviceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- MyBinder ourBinder = ((MyBinder) service);
- boundedService = ourBinder.binderService;
- boundedService.policeSirinna(activity);
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- }
- BoundedService
- =========================
- 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;
- public class BoundedService extends Service {
- protected Boolean isRun = false; // Boolean for Chacalaka methods
- Runnable light1, light2,light3;
- final int SLEEP = 200;
- LinearLayout red_color, blue_color;
- Activity activity;
- @Nullable
- @Override // Method to start the service
- public IBinder onBind(Intent intent)
- {
- Toast.makeText(this, "Service Was bounded", Toast.LENGTH_SHORT).show();
- MyBinder binder = new MyBinder();
- binder.binderService = this;
- isRun = true;
- return binder;
- }
- @Override // Method to stop the service
- public boolean onUnbind(Intent intent) {
- Toast.makeText(this, "Un Bind!\n stop", Toast.LENGTH_LONG).show();
- return super.onUnbind(intent);
- }
- // method to set the chakalaca lights
- public void policeSirinna(final Activity activity) {
- this.activity = activity;
- red_color = (LinearLayout) activity.findViewById(R.id.red_color);
- blue_color = (LinearLayout) activity.findViewById(R.id.blue_color);
- light1 = new Runnable() {
- @Override
- public void run() {
- red_color.setBackgroundColor(Color.RED);
- blue_color.setBackgroundColor(Color.BLUE);
- }
- };
- light2 = new Runnable() {
- @Override
- public void run() {
- red_color.setBackgroundColor(Color.BLUE);
- blue_color.setBackgroundColor(Color.RED);
- }
- };
- light3 = new Runnable() {
- @Override
- public void run() {
- red_color.setBackgroundColor(Color.WHITE);
- blue_color.setBackgroundColor(Color.WHITE);
- }
- };
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (isRun) {
- try {
- activity.runOnUiThread(light1);
- Thread.sleep(100);
- activity.runOnUiThread(light1);
- Thread.sleep(100);
- activity.runOnUiThread(light1);
- Thread.sleep(100);
- activity.runOnUiThread(light2);
- Thread.sleep(100);
- activity.runOnUiThread(light2);
- Thread.sleep(100);
- activity.runOnUiThread(light2);
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- }
- MyBinder
- =======================
- import android.os.Binder;
- public class MyBinder extends Binder {
- public BoundedService binderService;
- }
- activity_main.xml
- ==========================
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/polic_btn"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <Button
- android:id="@+id/on_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentStart="true"
- android:layout_alignParentTop="true"
- android:layout_marginStart="15dp"
- android:layout_marginTop="28dp"
- android:text="On"
- android:onClick="startMe"
- />
- <LinearLayout
- android:id="@+id/red_color"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:layout_alignTop="@+id/on_button"
- android:layout_marginStart="28dp"
- android:layout_toEndOf="@+id/on_button"
- android:orientation="horizontal"></LinearLayout>
- <LinearLayout
- android:id="@+id/blue_color"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:layout_alignTop="@+id/red_color"
- android:layout_marginStart="36dp"
- android:layout_toEndOf="@+id/red_color"
- android:orientation="horizontal"></LinearLayout>
- <Button
- android:id="@+id/off_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignStart="@+id/on_button"
- android:layout_below="@+id/on_button"
- android:text="Off"
- android:onClick="stopMe"/>
- </RelativeLayout>
Add Comment
Please, Sign In to add comment