5.22

所花时间(包括上课):2

打码量(行):230

博客量(篇):1

了解到知识点:学习Bound service

 

 package com.example.myapp;

 

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

 

public class BoundService extends Service {

 

    private static final String TAG = "BoundService";

    private final IBinder binder = new LocalBinder();

 

    public class LocalBinder extends Binder {

        BoundService getService() {

            return BoundService.this;

        }

    }

 

    @Override

    public IBinder onBind(Intent intent) {

        Log.d(TAG, "onBind: Service bound");

        return binder;

    }

 

    public int addNumbers(int num1, int num2) {

        return num1 + num2;

    }

 

    @Override

    public void onDestroy() {

        super.onDestroy();

        Log.d(TAG, "onDestroy: Service destroyed");

    }

}

package com.example.myapp;

 

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.util.Log;

import android.view.View;

import android.widget.Toast;

 

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private static final String TAG = "MainActivity";

    private BoundService boundService;

    private boolean isBound = false;

 

    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override

        public void onServiceConnected(ComponentName className, IBinder service) {

            BoundService.LocalBinder binder = (BoundService.LocalBinder) service;

            boundService = binder.getService();

            isBound = true;

            Log.d(TAG, "onServiceConnected: Service connected");

        }

 

        @Override

        public void onServiceDisconnected(ComponentName className) {

            isBound = false;

            Log.d(TAG, "onServiceDisconnected: Service disconnected");

        }

    };

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

 

    @Override

    protected void onStart() {

        super.onStart();

        Intent intent = new Intent(this, BoundService.class);

        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

    }

 

    @Override

    protected void onStop() {

        super.onStop();

        if (isBound) {

            unbindService(serviceConnection);

            isBound = false;

        }

    }

 

    public void performAddition(View view) {

        if (isBound) {

            int result = boundService.addNumbers(5, 3);

            Toast.makeText(this, "Addition result: " + result, Toast.LENGTH_SHORT).show();

        } else {

            Toast.makeText(this, "Service not bound", Toast.LENGTH_SHORT).show();

        }

    }

}

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <Button

        android:id="@+id/addButton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Perform Addition"

        android:onClick="performAddition"

        android:layout_centerInParent="true" />

 

</RelativeLayout>

posted @ 2024-05-22 21:57  赵千万  阅读(8)  评论(0编辑  收藏  举报