/*
*Service服务:与Activity最相似;都继承了Context;有自己的生命周期;也可作为可执行的程序.
*区别是运行在后台,不会有界面.
* 使用中如果需要用户交互应该用Activity;否则应该选用Service.
* 生命周期方法5个:onCreate() onBind() onStartCommand() onUnbind() onDestroy()
* onCreate() Service第一次实例化回调
* onBind() 该方法返回IBinder对象,用于应用程序与Service通信;必须重官吏的方法,通常继承Binder()子类来实现自己的IBinder对象.
* onStartCommand()当调用startService()方法启动Service时会回调方法.
* onUnbind()Service上的客户端都断开连接时回调
* onDestroy()Service被关闭时回调.
*
*同样使用Service需要继承基类或其子类 IntentService
* 使用步骤2
* 1 定义继承Service的子类,重写其周期方法 onBind()为必须重写的方法.
* 2 在AndroidManifest.xml中配置<service name:> 四大组件须显式配置.
*
* Android5.0开始,要求须显式Intent启动Service.
* 启动关闭(配套使用的)Service startService() stopService() 这样启动的Service与启动它的客户端没有什么联系.
* bindService() unBindService()这样启动的Service与客户端绑定在一起了,当
* 访问者退出,Service与退出.
*
* 当bindService(ServiceConnection)方法启动, onBinder()方法返回的IBinder对象会给到 ServiceConnection对象onServiceConnected()方法
*
* 当客户端连接到Service,onServiceConnected()被回调;异常中止回调onServiceDisconnected();(正常unBindService()不会回调)
*
*生命周期分2种:客户端调用startService()方法-->onCreate()回调-->onStartCommand()回调-->调用stopService()-->onDestroy()回调.
* 客户端调用bindService()方法-->onCreate()回调-->onBind()回调-->unBindService()-->onUnBind()回调-->onDestroy()回调
*
* Service有2缺陷: 与所在的应用为同一进程;不是单独的线程,不能进行耗时任务.
*
* IntentService:继承了Service, 5个优点:
* 1 用队列的形式来处理Intent.
* 2 单独的线程处理onHandlerIntent()方法
* 3 当Intent处理完成,自动停止.
* 4 重写了onBind()方法 onStartCommand()方法
* 5 使用IntentService只需要实现OnHandlerIntent()方法即可...(由于会单独线程,无须担心阻塞线程或ANR)
*
*
*
*
* */
package com.example.tyxiong.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
class myBinder extends Binder
{
public myBinder() {
}
}
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
Log.w("xxx", "onBind");
return new myBinder();
}
@Override
public void onCreate() {
super.onCreate();
Log.w("xxx", "onCreate: ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("xxx", "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.w("xxx", "onDestroy: ");
}
@Override
public boolean onUnbind(Intent intent) {
Log.w("xxx", "onUnbind: ");
return super.onUnbind(intent);
}
}
package com.example.tyxiong.myapplication;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
Intent intent;
private ServiceConnection connect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View view) {
intent = new Intent(this, MyService.class);
// startService(intent);
bindService(intent, connect = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {//当onBind()返回的对象不为null才会回调该方法哦.
Toast.makeText(MainActivity.this, "connection>>>", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, BIND_AUTO_CREATE);
}
public void stopService(View view) {
// stopService(intent);
unbindService(connect);
}
}
***********************************************
Service概述 生命周期 IBinder
最新推荐文章于 2024-05-15 11:21:58 发布