Service

  • 什么是service
  • service有哪些应用场景
  • startService方式启动service怎么做
  • bindService方式启动service怎么做
  • IntentService有什么不同
  • IntentService怎么用,注意事项

    什么是service
    1.Android四大组件之一
    2.与Activity级别相当
    3.长时间运行在后台是不可见的,没有界面的组件

service有哪些应用场景
远程操作和耗时操作

startService方式启动service怎么做
1.新建类继承Service
2.重写onCreate方法
Service在创建时调用的方法,可以用来做数据初始化,Service在没有停止前,只会执行一次
3.实现onBind抽象方法
由于Service是一个抽象类,定义了一个onBind抽象方法,所以必须实现此抽象方法,return null即可,此方法在startService方式用不上,无需理会即可。
4.重写onStartCommand方法
5.重写onDestroy方法
Service在退出时调用,此方法中可以编写释放资源的操作
6.在AndroidManifest中注册Service
使用标签注册同Activity注册
7.在有context环境在启动service

package com.example.a17708.pratice;

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

public class MyService extends Service{
    private int count=0;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (count<100){
                    Log.e("onStartCommand", "run: "+count );
                    count++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("onDestroy","销毁");
    }
}
package com.example.myapplication.myService;

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

import com.example.myapplication.R;

public class MyServiceActivity extends AppCompatActivity implements View.OnClickListener {
    private Button myservice_btn, myservice_start, myservice_stop;
    public static ImageView myservice_image;

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

        myservice_btn = findViewById(R.id.myservice_btn);
        myservice_btn.setOnClickListener(this);

        myservice_start = findViewById(R.id.myservice_start);
        myservice_start.setOnClickListener(this);

        myservice_stop = findViewById(R.id.myservice_stop);
        myservice_stop.setOnClickListener(this);

        myservice_image = findViewById(R.id.myservice_image);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.myservice_start:
                Intent intent = new Intent(MyServiceActivity.this, MyService.class);
                startService(intent);//启动service
                break;
            case R.id.myservice_stop:
                Intent intent2 = new Intent(MyServiceActivity.this, MyService.class);
                stopService(intent2);//停止service
                break;
            default:
                break;
        }
    }
}

bindService方式启动service怎么做
1.新建类继承Service
2.重写onCreate方法
3.实现onBind抽象方法
4.重写onUnbind方法
5.重写onDestory方法
6.在AndroidManifest中注册
7.在有context环境中实现ServiceConnection接口,重写onServiceConnected()和onServiceDisconnected()方法
8.在有context环境中通过bindService绑定Service
9.在有context环境中通过unbindService解绑Service

IntentService有什么不同

package com.example.myapplication.myIntentService;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;



public class MyIntentService extends IntentService{


    public MyIntentService() {
        super("");
    }


    public MyIntentService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        int count=0;
        while (count<100){
            count++;
            Log.d("MyIntentService", "onHandleIntent: "+count);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
package com.example.myapplication.myIntentService;

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


public class MyIntentServiceActivity extends AppCompatActivity implements View.OnClickListener{
    private Button myIntentservice_start, myIntentservice_stop;

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

        myIntentservice_start = findViewById(R.id.myIntentservice_start);
        myIntentservice_start.setOnClickListener(this);

        myIntentservice_stop = findViewById(R.id.myIntentservice_stop);
        myIntentservice_stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.myIntentservice_start:
                Intent intent = new Intent(MyIntentServiceActivity.this, MyIntentService.class);
                startService(intent);//启动service
                break;
            case R.id.myIntentservice_stop:
                Intent intent2 = new Intent(MyIntentServiceActivity.this, MyIntentService.class);
                stopService(intent2);//停止service
                break;
                default:
                    break;
        }

    }
}

IntentService怎么用,注意事项
启动Service只能用start启动Service

你可能感兴趣的:(Android)