Advertisement
Guest User

bounded services

a guest
Nov 17th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.05 KB | None | 0 0
  1. activity_main.xml
  2. =====================
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     android:gravity="center" >
  8.  
  9.     <Button
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:textSize="32sp"
  13.         android:text="Start"
  14.         android:background="#055A05"
  15.         android:textColor="#fff"
  16.         android:layout_marginBottom="30dp"
  17.         android:id="@+id/btnStart"
  18.         />
  19.  
  20.     <Button
  21.         android:layout_width="match_parent"
  22.         android:layout_height="wrap_content"
  23.         android:textSize="32sp"
  24.         android:text="Stop"
  25.         android:background="#E92020"
  26.         android:textColor="#fff"
  27.         android:id="@+id/btnStop"
  28.         />
  29. </LinearLayout>
  30.  
  31.  
  32. MainActivity.java
  33. =========================
  34. package com.wtf.myboundedservices;
  35.  
  36. import androidx.appcompat.app.AppCompatActivity;
  37.  
  38. import android.content.ComponentName;
  39. import android.content.Context;
  40. import android.content.Intent;
  41. import android.content.ServiceConnection;
  42. import android.os.Bundle;
  43. import android.os.IBinder;
  44. import android.view.View;
  45. import android.widget.Toast;
  46.  
  47. public class MainActivity extends AppCompatActivity {
  48.     Context context;
  49.     boolean isConnected=false; //indicates if our service is bounded or not
  50.     //Activit's instance variable to contain our bounded service
  51.     BoundedService boundedService;
  52.  
  53.     //service connection used to catch bounded service from caller side
  54.     ServiceConnection serviceConnection = new ServiceConnection() {
  55.         @Override
  56.         public void onServiceConnected(ComponentName name, IBinder service) {
  57.             //my binder is a container for bounded service (כרטיס כספומט)
  58.             MyBinder caspomat = (MyBinder)service;
  59.             //connect to bounded service using our binder
  60.             boundedService = caspomat.bindService;
  61.             boundedService.firstBinding();
  62.         }
  63.  
  64.         @Override
  65.         public void onServiceDisconnected(ComponentName name) {
  66.  
  67.         }
  68.     };
  69.  
  70.     @Override
  71.     protected void onCreate(Bundle savedInstanceState) {
  72.         super.onCreate(savedInstanceState);
  73.         setContentView(R.layout.activity_main);
  74.         setPointer();
  75.     }
  76.  
  77.     private void setPointer() {
  78.         this.context=this;
  79.         final Intent myBoundedService = new Intent(this,BoundedService.class);
  80.         findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() {
  81.             @Override
  82.             public void onClick(View v) {
  83.                 if (!isConnected){
  84.                     isConnected=true;
  85.                     bindService(myBoundedService,serviceConnection,Context.BIND_AUTO_CREATE);
  86.                 } else {
  87.                         Toast.makeText(context, "Rimon, service is already connected!!", Toast.LENGTH_SHORT).show();
  88.                 }
  89.             }
  90.         });
  91.         findViewById(R.id.btnStop).setOnClickListener(new View.OnClickListener() {
  92.             @Override
  93.             public void onClick(View v) {
  94.                 if (isConnected){
  95.                     unbindService(serviceConnection);
  96.                 }
  97.             }
  98.         });
  99.     }
  100. }
  101.  
  102.  
  103. MyBinder.java
  104. ==============
  105. package com.wtf.myboundedservices;
  106.  
  107. import android.os.Binder;
  108.  
  109. public class MyBinder extends Binder {
  110.     public BoundedService bindService;
  111.  
  112. }
  113.  
  114.  
  115. BoundedService.java
  116. =========================
  117. package com.wtf.myboundedservices;
  118.  
  119. import android.app.Service;
  120. import android.content.Intent;
  121. import android.os.IBinder;
  122. import android.util.Log;
  123. import android.widget.Toast;
  124.  
  125. import androidx.annotation.Nullable;
  126.  
  127. public class BoundedService extends Service {
  128.  
  129.     @Nullable
  130.     @Override
  131.     public IBinder onBind(Intent intent) {
  132.         Toast.makeText(this, "service is bounded", Toast.LENGTH_SHORT).show();
  133.         //create a binder for data transfer
  134.         MyBinder binder = new MyBinder();
  135.         binder.bindService=this; //MyBinder -> container to transfer bounded services to the activity
  136.         return binder;
  137.     }
  138.  
  139.     @Override
  140.     public boolean onUnbind(Intent intent) {
  141.         Toast.makeText(this, "love to see you go, hate to see you leave", Toast.LENGTH_LONG).show();
  142.         return super.onUnbind(intent);
  143.     }
  144.  
  145.     @Override
  146.     public void onRebind(Intent intent) {
  147.         Toast.makeText(this, "Welcome back Shani", Toast.LENGTH_LONG).show();
  148.         super.onRebind(intent);
  149.     }
  150.    
  151.     public boolean uploadPicture(String fileName){
  152.         try {
  153.             Toast.makeText(this, "pic " + fileName + " was uploaded to Rimon ...", Toast.LENGTH_SHORT).show();
  154.             return true;
  155.         } catch (Exception e){
  156.             Log.e("err", "uploadPicture: "+e.getMessage());
  157.         }
  158.         return false;
  159.     }
  160.    
  161.     public void uskut (String name){
  162.         Toast.makeText(this, "Uskut !!! "+name+", Ya zalama!!!!", Toast.LENGTH_SHORT).show();
  163.     }
  164.    
  165.     public void firstBinding(){
  166.         Toast.makeText(this, "enter Metronit code", Toast.LENGTH_SHORT).show();
  167.     }
  168. }
  169.  
  170.  
  171. Manifest.xml
  172. ==================
  173. <?xml version="1.0" encoding="utf-8"?>
  174. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  175.     xmlns:tools="http://schemas.android.com/tools"
  176.     package="com.wtf.myboundedservices">
  177.  
  178.     <application
  179.         android:allowBackup="true"
  180.         android:icon="@mipmap/ic_launcher"
  181.         android:label="@string/app_name"
  182.         android:roundIcon="@mipmap/ic_launcher_round"
  183.         android:supportsRtl="true"
  184.         android:theme="@style/AppTheme"
  185.         tools:ignore="GoogleAppIndexingWarning">
  186.         <activity android:name=".MainActivity">
  187.             <intent-filter>
  188.                 <action android:name="android.intent.action.MAIN" />
  189.  
  190.                 <category android:name="android.intent.category.LAUNCHER" />
  191.             </intent-filter>
  192.         </activity>
  193.         <service android:name=".BoundedService"/>
  194.     </application>
  195.  
  196. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement