android Service的学习

1.以前刚接触Service的时候,很不理解。但是用过俩三次就慢慢理解了,可是理解是理解了,但是记得不清楚,长时间不用就忘记了,所以我总结下Service的用法,加强自己的印象。


2.Service作为android 四大组件之一,用到的地方还是比较多的,Service没有界面,类似于电脑中的后台。Service的启动方式有俩种:bindService 和StartService俩种方法,这俩种方法的生命周期是不一样的。


startService:生命周期


点击了 一次startservice之后,再点击一次或者多次。只会执行onstart方法,不会额外的在执行oncreate方法。

但是假如 你stopserevice之后,在startservice ,那么回执行oncreate -------〉onstart。注意startservice启动的service,在activity结束后无法结束其service,除非整个应用的进程关闭了。


绑定式启动Service:


bindservice:顾名思义绑定式service,主要意思就是说  该service 与activity绑定。因此activity的生命周期会对service的生命周期产生影响。当activity   被finish掉,那么那个service也会被干掉。

而且 service  启动之后,你在按下 启动按钮,是没得什么效果的,要想重新在此启动,那么就需要解除绑定。也就是调用unbindservice方法。那么service 就会执行其 onunbind方法 和ondestroy方法。


在这里说下onBind方法:

 @Override
    public IBinder onBind(Intent intent) {
        Log.d("123456789", "onBind");
        return new MyBinder();
    }

    public class MyBinder extends Binder{

       public String sayhello(String name)
       {
           return "hello"+name;
       }
    }
onBind方法要求返回一个IBinder对象,Binder 类是IBinder的子类,MyBinder这个类 就是我们的业务逻辑处理类。例如这里面我设置了一个sayhello()方法.

MyService写好后,那么可以直接在activity中调用。代码如下:

package com.example.lenovo.androidservicetest;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Intent intent;
    Button startService;
    Button stopService;
    Button bindService;
    Button unbindService;
    Button toActivity;

    <span style="color:#ffff33;">ServiceConnection </span>connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            MyService.MyBinder binder=(MyService.MyBinder )service;
            String s=binder.sayhello("wxy");
            Toast.makeText(MainActivity.this,s, Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService= (Button) findViewById(R.id.start);
        stopService= (Button) findViewById(R.id.stop);
        bindService= (Button) findViewById(R.id.bindService);
        unbindService= (Button) findViewById(R.id.unbind);
        toActivity= (Button) findViewById(R.id.toSecondActivity);
        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);
        toActivity.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.start:
                 intent=new Intent();
                intent.setPackage("com.example.lenovo.androidservicetest");
                intent.setAction("android.intent.action.myservice");
                startService(intent);
                break;
            case R.id.stop:
                stopService(intent);
                break;
            case R.id.bindService:
                Intent intent2=new Intent();
                intent2.setPackage("com.example.lenovo.androidservicetest");
                intent2.setAction("android.intent.action.myservice");
              <span style="background-color: rgb(255, 255, 51);">  bindService(intent2,connection,Context.BIND_AUTO_CREATE);</span>
                break;
            case R.id.unbind:
                unbindService(connection);
                break;
            case R.id.toSecondActivity:
                Intent intent1=new Intent();
                intent1.setClass(MainActivity.this,Second.class);
                startActivity(intent1);
               // finish();
                break;

        }

    }
}

bindservice 这个方法中有三个参数,intent,ServiceConnection对象,和 int 类型的flag。因此我们需要new 一个ServiceConnection对象,具体代码如上所示。

在onServiceConnected(),方法中 我们可以把IBinder service 强行转化为MyBinder,这样我们就可以通过MyBinder对象来调用我们需要的方法。


基本内容就是这些。Service面试经常考到,所以在此复习下。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值