绑定本地服务 bind 调用 server 方法
  HvTJUzsxOBtS 2023年11月25日 49 0



文章目录

  • 1、简介
  • 2、功能实现


1、简介

通过bind 绑定本地服务,并且通过bind 访问服务里面的方法
通过点击 按钮,通过bind 调用服务里的方法

bind 绑定的服务生命周期 会随着调用者的结束而结束,也可以主动结束

绑定本地服务 bind 调用 server 方法_ide

2、功能实现

1)目录结构

绑定本地服务 bind 调用 server 方法_service_02

2)、AndroidManifest.xml
添加服务

<service android:name=".MyService"/>

3)activity_main.xml 布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_bind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="绑定服务" />
    <Button
        android:id="@+id/btn_bind_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="服务显示" />


    <Button
        android:id="@+id/btn_unbind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="解绑服务" />

</LinearLayout>

4) mainActivity.java

package myapplication21.lum.com.mytestview;

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.util.Log;
import android.view.View;

public class MainActivity extends Activity {
	private String TAG  = "MainActivity: ";
	private ServiceConnection conn;
	private MyService.MyServiceBinder binder;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		conn=new ServiceConnection() {
			/**
			 * 当service 崩溃或系统被强制杀死的时候调用
			 * */
			@Override
			public void onServiceDisconnected(ComponentName name) {
				Log.d(TAG,"onServiceDisconnected");
			}
			/**
			 * 当服务访问者与服务绑定成功后调用
			 * */
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				Log.d(TAG,"onServiceConnected");
				binder = (MyService.MyServiceBinder)service;
			}
		};
	}

	public void onClick(View view) {
		Intent service=new Intent();
		service.setClass(this,MyService.class);
		switch (view.getId()) {
		case R.id.btn_bind://绑定服务
			bindService(service, conn,Context.BIND_AUTO_CREATE);
			break;
			case R.id.btn_bind_show://bind 调用服务的 函数
				binder.show();
				break;
		case R.id.btn_unbind:// 解绑服务
			unbindService(conn);
			break;
		}
	}
}
  1. MyService.java 文件
package myapplication21.lum.com.mytestview;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
	private String TAG = "MyService: ";
	/**
	 * 服务绑定后调用
	 * */
	@Override
	public IBinder onBind(Intent intent) {
		Log.d(TAG, "onBind");
		return new MyServiceBinder();
	}

	@Override
	public void onCreate() {
		Log.d(TAG, "onCreate");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy");
	}
	/**
	 * 服务被解绑后调用
	 * */
	@Override
	public boolean onUnbind(Intent intent) {
		Log.d(TAG, "onUnbind");
		return super.onUnbind(intent);
	}

	@Override
	public void onRebind(Intent intent) {
		Log.d(TAG, "onRebind");
	}

	public void show() {
		Log.d(TAG, "service....show");
	}

	/**
	 * 中介
	 * */
	public class MyServiceBinder extends Binder {
		public void show() {
			MyService.this.show();
		}
	}
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月25日 0

暂无评论

推荐阅读
HvTJUzsxOBtS