安卓案例:绑定和解绑服务
一、运行效果
二、实现步骤
1、创建安卓应用BindUnbindService
2、准备背景图片background.jpg,放到mipmap目录里
3、布局资源文件activity_main.xml
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
绑定与解绑服务
绑定服务
解绑服务
5、创建自定义服务类CustomService
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里注册服务
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
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、运行程序,看看结果
单击【解绑服务】按钮,看看LogCat窗口:
9、绑定和解绑服务小结
(1)onCreate()——只执行一次,做初始化工作
(2)onBind()——只执行一次,做业务工作
(3)onDestroy()——只执行一次,做扫尾工作。
对于上述案例程序,如果单击两次【绑定服务】再单击一次【解绑服务】,结果如下:
有个小问题,没有执行onDestroy()方法,为什么?留给大家思考。
本文分享 CSDN - howard2005。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。