安卓-服务中onCreate与onStartCommand的区别

服务生命周期中主要有三个重要的阶段:

1)创建服务 onCreate

2)开始服务 onStartCommand

3)销毁服务  onDestroy

一个服务只会创建一次,销毁一次,但是会开始多次

下面用一个demo来演示下:

界面中有三个按钮,一次是启动服务,再次启动服务,结束服务,演示顺序,先点击启动服务按钮,再点击再次启动服务按钮,最后点击结束服务的按钮,通过打印Log来看执行的对应的方法。

1)点击启动服务

I/BaseService: onCreate: 
I/BaseService: onStartCommand: 

2)点击再次启动服务

I/BaseService: onCreate: 
I/BaseService: onStartCommand: 
I/BaseService: onStartCommand: 

3)点击结束服务

I/BaseService: onCreate: 
I/BaseService: onStartCommand: 
I/BaseService: onStartCommand: 
I/BaseService: onDestroy:

由以上的测试结果可以看出,结论是正确的。

演示代码如下:

activity_service_base.xml



    

主Activity界面ServiceBaseActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.mobile.cdtx.blog.R;

public class ServiceBaseActivity extends AppCompatActivity implements View.OnClickListener{
    Button btnStart,btnReStart,btnEnd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_base);
        initView();//控件初始化
    }

    //控件的初始化
    private void initView(){
        btnStart = (Button)findViewById(R.id.id_btn_start);
        btnReStart = (Button)findViewById(R.id.id_btn_restart);
        btnEnd = (Button)findViewById(R.id.id_btn_end);
        btnStart.setOnClickListener(this);
        btnReStart.setOnClickListener(this);
        btnEnd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.id_btn_start://启动服务
                onStartService();
                break;
            case R.id.id_btn_restart://再次启动服务
                onReStartService();
                break;
            case R.id.id_btn_end://结束服务
                onEndService();
                break;
            default:
                break;
        }
    }

    //启动服务
    private void onStartService(){
        Intent intent = new Intent(ServiceBaseActivity.this,BaseService.class);
        startService(intent);
    }

    //再次启动服务
    private void onReStartService(){
        Intent intent = new Intent(ServiceBaseActivity.this,BaseService.class);
        startService(intent);
    }

    //结束服务
    private void onEndService(){
        Intent intent = new Intent(ServiceBaseActivity.this,BaseService.class);
        stopService(intent);
    }

}

服务代码BaseService.java:

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

public class BaseService extends Service {
    private static final String TAG = "BaseService";
    public BaseService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

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

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

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

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.i(TAG, "onRebind: ");
        super.onRebind(intent);
    }
}

注意服务要在AndroidManifest.xml中注册

代码部分如下:


        


你可能感兴趣的:(安卓)