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:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center" >
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="32sp"
- android:text="Start"
- android:background="#055A05"
- android:textColor="#fff"
- android:layout_marginBottom="30dp"
- android:id="@+id/btnStart"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="32sp"
- android:text="Stop"
- android:background="#E92020"
- android:textColor="#fff"
- android:id="@+id/btnStop"
- />
- </LinearLayout>
- MainActivity.java
- =========================
- package com.wtf.myboundedservices;
- import androidx.appcompat.app.AppCompatActivity;
- 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.view.View;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- Context context;
- boolean isConnected=false; //indicates if our service is bounded or not
- //Activit's instance variable to contain our bounded service
- BoundedService boundedService;
- //service connection used to catch bounded service from caller side
- ServiceConnection serviceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- //my binder is a container for bounded service (כרטיס כספומט)
- MyBinder caspomat = (MyBinder)service;
- //connect to bounded service using our binder
- boundedService = caspomat.bindService;
- boundedService.firstBinding();
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- final Intent myBoundedService = new Intent(this,BoundedService.class);
- findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (!isConnected){
- isConnected=true;
- bindService(myBoundedService,serviceConnection,Context.BIND_AUTO_CREATE);
- } else {
- Toast.makeText(context, "Rimon, service is already connected!!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- findViewById(R.id.btnStop).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isConnected){
- unbindService(serviceConnection);
- }
- }
- });
- }
- }
- MyBinder.java
- ==============
- package com.wtf.myboundedservices;
- import android.os.Binder;
- public class MyBinder extends Binder {
- public BoundedService bindService;
- }
- BoundedService.java
- =========================
- package com.wtf.myboundedservices;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- import android.widget.Toast;
- import androidx.annotation.Nullable;
- public class BoundedService extends Service {
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- Toast.makeText(this, "service is bounded", Toast.LENGTH_SHORT).show();
- //create a binder for data transfer
- MyBinder binder = new MyBinder();
- binder.bindService=this; //MyBinder -> container to transfer bounded services to the activity
- return binder;
- }
- @Override
- public boolean onUnbind(Intent intent) {
- Toast.makeText(this, "love to see you go, hate to see you leave", Toast.LENGTH_LONG).show();
- return super.onUnbind(intent);
- }
- @Override
- public void onRebind(Intent intent) {
- Toast.makeText(this, "Welcome back Shani", Toast.LENGTH_LONG).show();
- super.onRebind(intent);
- }
- public boolean uploadPicture(String fileName){
- try {
- Toast.makeText(this, "pic " + fileName + " was uploaded to Rimon ...", Toast.LENGTH_SHORT).show();
- return true;
- } catch (Exception e){
- Log.e("err", "uploadPicture: "+e.getMessage());
- }
- return false;
- }
- public void uskut (String name){
- Toast.makeText(this, "Uskut !!! "+name+", Ya zalama!!!!", Toast.LENGTH_SHORT).show();
- }
- public void firstBinding(){
- Toast.makeText(this, "enter Metronit code", Toast.LENGTH_SHORT).show();
- }
- }
- Manifest.xml
- ==================
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- package="com.wtf.myboundedservices">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme"
- tools:ignore="GoogleAppIndexingWarning">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service android:name=".BoundedService"/>
- </application>
- </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement