21、Android -- Service 基础学习

说明

服务没有单独界面,后台提供前台运行的服务

  • 新建Java类,直接继承Service后的类就是服务类
  • AndroidManifest.xml需要声明下:
<service android:name=".service.TestService"/>

生命周期

Service有两种其用法格式:startService、bindService。
根据启动方式不同,Service的生命周期也略有不同

startService启动方式

启用

	startService(new Intent(this,TestService.class));

停止

	stopService(new Intent(this,TestService.class));

生命周期:
1、onCreate()
2、onStartCommand()
3、onStart() --一般不用了
4、服务运行中
5、onDestroy()
6、服务停止

bindService启动方式

bindService启用需要一个桥梁,所以
首先、建立桥梁

 private ServiceConnection connect = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

启用

	bindService(new Intent(MainActivity.this,TestService.class),connect, Context.BIND_AUTO_CREATE);

停止

	unbindService(connect);

生命周期
1、onCreate()
2、onBind()
3、服务运行中
4、onUnbind()
5、onDestroy()
6、服务停止

startService 与 bindService区别

bindService:Service启用后,会随Activity的销毁而销毁。
startService :Service启用后,Activity的销毁时,Service不销毁

示例

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_one"
        android:text="startService"
        android:layout_width="200dp"
        android:layout_height="50dp"
        />
    <Button
        android:id="@+id/btn_two"
        android:text="stopService"
        android:layout_width="200dp"
        android:layout_height="50dp"
        />
    <Button
        android:id="@+id/btn_three"
        android:text="bindService"
        android:background="@color/teal_200"
        android:layout_width="200dp"
        android:layout_height="50dp"
        />
    <Button
        android:id="@+id/btn_four"
        android:text="unBindService"
        android:layout_marginTop="10dp"
        android:background="@color/teal_200"
        android:layout_width="200dp"
        android:layout_height="50dp"
        />

</LinearLayout>

java/com/pha/three/service/TestService.java

package com.pha.three.service;

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

import androidx.annotation.Nullable;

public class TestService extends Service {
    private static final String TAG = TestService.class.getSimpleName();

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


    @Override
    public void onStart(Intent intent, int startId) {
        Log.e(TAG, "onStart: " );
        super.onStart(intent, startId);
    }

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

    }

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

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind: " );
        return null;
    }

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

java/com/pha/three/MainActivity.java

package com.pha.three;

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 com.pha.three.service.TestService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_one:
                startService(new Intent(this,TestService.class));
                break;
            case R.id.btn_two:
                stopService(new Intent(this,TestService.class));
                break;
            case R.id.btn_three:
                BindService();
                break;
            case R.id.btn_four:
                UnBindService();
                break;
            default:
                break;
        }
    }

    //Activity 和Service  桥梁
    private ServiceConnection connect = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    private void BindService(){
        bindService(new Intent(MainActivity.this,TestService.class),connect, Context.BIND_AUTO_CREATE);
    }
    private void UnBindService(){
        unbindService(connect);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值