public class MyService extends Service {
// 必须实现,绑定该Service时被回调
@Override
public IBinder onBind(Intent intent) {
return null;
}
// Service被创建时回调
@Override
public void onCreate() {
super.onCreate();
// 定义相关业务逻辑
System.out.println("Service is Created");
}
// Service被启动时回调
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 定义相关业务逻辑
System.out.println("Service is Started");
return START_STICKY;
}
// Service被关闭之前回调
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service is Destroyed");
}
}
<application
...
<!-- 配置一个Service组件 -->
<service android:name=".MyService">
<intent-filter>
<!-- 为该Service组件的intent-filter配置action -->
<action android:name="com.gc.service.MY_SERVICE" />
</intent-filter>
</service>
</application>
// 创建启动Service的Intent
final Intent intent = new Intent();
// 为Intent设置Action属性
intent.setAction("com.gc.service.MY_SERVICE");
...
// 启动指定Serivce
startService(intent);
...
// 停止指定Serivce
stopService(intent);
bindService(Intent intent, ServiceConnection conn, int flags),三个参数如下: intent:指定要启动的Service conn:用于监听访问者与Service之间的连接情况,当访问者与Service之间连接成功时将回调该ServiceConnection对象的onServiceConnected(ComponentName name, IBinder service)方法;反之回调该ServiceConnection对象的onServiceDisconnected(ComponentName name)方法(主动调用unbindService方法断开连接时则不回调) flags:指定绑定时是否创建Service,0:不自动创建;BIND_AUTO_CREATE:自动创建 注意:ServiceConnection对象的onServiceConnected方法中有一个IBinder对象,该对象即可实现与绑定Service之间的通信。 在绑定本地Service的情况下,onBind(Intent intent)方法所返回的IBinder对象将会传给ServiceConnection对象里onServiceConnected(ComponentName name, IBinder service)方法的service参数,这样访问者就可以通过该IBinder对象与Service进行通信。
public class MyService extends Service {
private int count;
// 定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
// 通过继承Binder来实现IBinder类
public class MyBinder extends Binder {
public int getCount() {
return count; // 获取Service的运行状态
}
}
// 必须实现,绑定该Service时被回调
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service is Binded");
return binder; // 返回IBinder对象
}
// Service被创建时回调
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service is Created");
count = 100;
}
// Service被断开连接时回调
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Service is Unbinded");
return true;
}
// Service被关闭之前回调
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service is Destroyed");
}
}
public class MyServiceTest extends Activity {
// Service的IBinder对象
MyService.MyBinder binder;
// 定义一个ServiceConnection对象
private ServiceConnection conn = new ServiceConnection() {
// 当该Activity与Service连接成功时回调
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 获取Service的onBind方法所返回的MyBinder对象
binder = (MyService.MyBinder) service;
}
// 当该Activity与Service断开连接时回调
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// 创建启动Service的Intent
final Intent intent = new Intent();
// 为Intent设置Action属性
intent.setAction("com.gc.service.MY_SERVICE");
// 绑定指定Serivce
bindService(intent, conn, Service.BIND_AUTO_CREATE);
...
binder.getCount(); // 获取Serivce的count值
...
// 解除绑定Serivce
unbindService(conn);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有