Android基础之服务的创建启动和停止

1.创建一个服务,让其继承自Serivce,并重写onBind方法,服务的创建是通过onCreate()方法实现,启动通过onStartCommand()方法,停止服务通过onDestory()方法。
2.没创建一个服务都会调用onCreate(),onStartCommand()方法,但是当一直启动服务,并且次数增加时,就会只调用onStartCommand()方法了,服务的停止调用onDestory()方法,停止后启动服务仍会调用onCreate(),onStartCommand()方法。
下面通过打印日志查看服务的创建启动停止过程。

package com.example.apple.service.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

    private final String TAG="MyService";

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG,"onCreate myService");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"onStartCommand myService");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy myService");
    }
}

MainActivity.class:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_start;
    private Button btn_stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_start=(Button)findViewById(R.id.start_service);
        btn_stop=(Button)findViewById(R.id.stop_service);

        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start_service:
                startService(new Intent(this, MyService.class));
                break;

            case R.id.stop_service:
                stopService(new Intent(this,MyService.class));
                break;
        }
    }
}

效果图:
这里写图片描述
Logcat打印日志:

06-27 09:08:04.023 2544-2544/com.example.apple.service D/MyService: onCreate myService
06-27 09:08:04.025 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:07.586 2544-2544/com.example.apple.service D/MyService: onDestroy myService
06-27 09:08:09.821 2544-2544/com.example.apple.service D/MyService: onCreate myService
06-27 09:08:09.822 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:11.246 2544-2544/com.example.apple.service D/MyService: onDestroy myService
06-27 09:08:13.132 2544-2544/com.example.apple.service D/MyService: onCreate myService
06-27 09:08:13.137 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:14.673 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:16.858 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:17.703 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:18.460 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:23.127 2544-2544/com.example.apple.service D/MyService: onDestroy myService
06-27 09:08:25.128 2544-2544/com.example.apple.service D/MyService: onCreate myService
06-27 09:08:25.140 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值