android 绑定服务 解绑服务,安卓案例:绑定和解绑服务

安卓案例:绑定和解绑服务

一、运行效果

22ec864860441122439a91cd97c9107f.png

二、实现步骤

1、创建安卓应用BindUnbindService

5357eef2567d856724708d8e74af0b54.png

2、准备背景图片background.jpg,放到mipmap目录里

ebb583d93d104796590b2afd96359aa1.png

3、布局资源文件activity_main.xml

7fa1f910128dd44e83c6f451f839d9c4.png

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@mipmap/background"

android:gravity="center"

tools:context="net.hw.bind_unbind_service.MainActivity">

android:id="@+id/btn_bind_service"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/bind_service"

android:onClick="doBindService"/>

android:id="@+id/btn_unbind_service"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/unbind_service"

android:onClick="doUnbindService"/>

4、字符串资源文件strings.xml

ce6fbf522a3fc26d9230450ec6fa5e5c.png

绑定与解绑服务

绑定服务

解绑服务

5、创建自定义服务类CustomService

ac5dee2bfd3a7a6e286e036610fccdc3.png

package net.hw.bind_unbind_service;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

import android.widget.Toast;

/**

* Created by ied on 2017/10/8.

*/

public class CustomService extends Service {

/**

* 应用程序标记

*/

private static final String TAG = "bind_unbind_service";

@Override

public IBinder onBind(Intent intent) {

Log.d(TAG, "CustomService.onBind()");

// 获取从前台窗口传递过来的数据

String message = intent.getStringExtra("message");

// 通过吐司在前台窗口显示获取数据

Toast.makeText(getApplicationContext(), "恭喜,成功绑定服务!\n\n主窗口传递的数据:"

+ message, Toast.LENGTH_SHORT).show();

// 在调试窗口输出信息

Log.i(TAG, "恭喜,成功绑定服务!\n\n主窗口传递的数据:" + message);

// 返回绑定器对象

return new Binder();

}

@Override

public void onCreate() {

super.onCreate();

Log.d(TAG, "CustomService.onCreate()");

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.d(TAG, "CustomService.onStartCommand()" + CustomService.this.hashCode() + ": " + startId);

return Service.START_NOT_STICKY;

}

@Override

public void onDestroy() {

super.onDestroy();

Log.d(TAG, "CustomService.onDestroy()");

}

}

6、在项目清单文件AndroidManifest.xml里注册服务

7af1317861644ceceb451a387124e3ad.png

package="net.hw.bind_unbind_service">

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">

7、主界面类MainActivity

2483f6e1a0c65992fdf25dd40c1c3e9b.png

package net.hw.bind_unbind_service;

import android.app.Activity;

import android.app.Service;

import android.content.ComponentName;

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 static final String TAG = "bind_unbind_service";

/**

* 服务连接对象

*/

private ServiceConnection conn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// 利用布局资源文件设置用户界面

setContentView(R.layout.activity_main);

}

/**

* 绑定服务按钮单击事件处理方法

*

* @param view

*/

public void doBindService(View view) {

// 创建意图,显式指定要绑定的服务

Intent intent = new Intent(MainActivity.this, CustomService.class);

// 让意图携带数据

intent.putExtra("message", "安卓开发真有趣~");

// 创建服务连接对象

conn = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

Log.d(TAG, "服务已经连接~");

}

@Override

public void onServiceDisconnected(ComponentName name) {

Log.d(TAG, "服务断开连接~");

}

};

// 按意图绑定服务

bindService(intent, conn, Service.BIND_AUTO_CREATE);

}

/**

* 解绑服务按钮单击事件处理方法

*

* @param view

*/

public void doUnbindService(View view) {

// 判断是否建立了服务连接

if (conn != null) {

// 解绑服务

unbindService(conn);

}

}

/**

* 销毁窗口时解绑服务

*/

@Override

protected void onDestroy() {

super.onDestroy();

// 判断是否建立了服务连接

if (conn != null) {

// 解绑服务

unbindService(conn);

}

}

}

8、运行程序,看看结果

52385fff22117f684da532e7549b0efc.png

d7d96e803ed94ddb3b08db6a39bbc6ce.png

单击【解绑服务】按钮,看看LogCat窗口:

072d0e3fb70246a5562908545226b308.png

9、绑定和解绑服务小结

(1)onCreate()——只执行一次,做初始化工作

(2)onBind()——只执行一次,做业务工作

(3)onDestroy()——只执行一次,做扫尾工作。

对于上述案例程序,如果单击两次【绑定服务】再单击一次【解绑服务】,结果如下:

52de3b5ecde555a4c0d7075eb5127952.png

有个小问题,没有执行onDestroy()方法,为什么?留给大家思考。

本文分享 CSDN - howard2005。

如有侵权,请联系 support@oschina.cn 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值