Android中的Service基础

时间:2022-12-14 18:00:52

Service主要用于后台程序和跨进程访问,可以在不显示界面的前提下完成任务,不影响用户的其他操作。

这里我展示一些基本的用法

新建一个Service类

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    private IBinder myBinder=new Binder(){
        public String getInterfaceDescriptor(){
            return "MyServerceUtil class.";
        }

    };
    public IBinder onBind(Intent intent)//绑定时触发
    {
        Toast.makeText(getApplicationContext(),"服务绑定",Toast.LENGTH_SHORT).show();
        return myBinder;
    }
    public void onRebind(Intent intent)//重新绑定时触发
    {
        super.onRebind(intent);
    }
    public boolean onUnbind(Intent intent)//解除绑定时触发
    {Toast.makeText(getApplicationContext(),"服务解绑",Toast.LENGTH_SHORT).show();
        return super.onUnbind(intent);
    }



    public void onCreate(){
        super.onCreate();

    }//创建时触发
    public void onDestroy(){
        super.onDestroy();
        Toast.makeText(getApplicationContext(),"服务销毁",Toast.LENGTH_SHORT).show();
    }

    public int onStartCommand(Intent intent,int flags,int startId)//启动时触发
    {
        Toast.makeText(getApplicationContext(),"服务启动",Toast.LENGTH_SHORT).show();

        return  Service.START_NOT_STICKY;
    }
}

  在Mainactivity中调用

package com.example.service;

import androidx.appcompat.app.AppCompatActivity;

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.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button start=null;
private Button bind=null;
private Button stop=null;
private Button unbind=null;

private ServiceConnection serviceConnection =new ServiceConnection() {
        @Override
        //连接
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        //断开连接
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start=(Button)super.findViewById(R.id.start);
        stop=(Button)super.findViewById(R.id.stop);
        bind=(Button)super.findViewById(R.id.bind);
        unbind=(Button)super.findViewById(R.id.unbind);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.startService(new Intent(MainActivity.this,MyService.class));
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.stopService(new Intent(MainActivity.this,MyService.class));
            }
        });
    bind.setOnClickListener(new View.OnClickListener() {
    @Override
     public void onClick(View v) {
        //绑定
    MainActivity.this.bindService(new Intent(MainActivity.this,MyService.class),
            MainActivity.this.serviceConnection, Context.BIND_AUTO_CREATE);
    }
});
    unbind.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //解除绑定
            MainActivity.this.unbindService(MainActivity.this.serviceConnection);
        }
    });
    }
}

  

服务的onCreate()函数在服务第一次启动时会执行一次,之后若不销毁的话再次启动就不会执行,只执行onStartCommand

绑定的用途是把Activity与Service绑定在一起,绑定后当Activity停止时,Service也会停止;未绑定时Activity退出后,Service会仍然运行。