Service概述 生命周期 IBinder


/*
*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);
    }
}
***********************************************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋葵好吃吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值