【安卓】- Service详讲

简介: 博客迁移,这是我在大学时候学校安卓,对于服务这一块的理解与心得,提供了详细的介绍。

Android Service

😄生命不息,写作不止
🔥 继续踏上学习之路,学之分享笔记
👊 总有一天我也能像各位大佬一样
🏆 一个有梦有戏的人 @怒放吧德德
🌝分享学习心得,欢迎指正,大家一起学习成长!

安卓.jpg

服务(Service)

是Android中的四大组件之一,它能够长期在后台运行,服务是不需要提供用户界面。即使用户切到另一应用程序,服务仍可以在后台运行。

1.Service 的创建

1.Service 的创建
服务创建就是新建一个java类,让他继承Service,添加未实现的方法

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

2.在清单文件中配置
其实这就类似javaweb在web.xml配置过滤器一样,在AndroidManifest.xml文件中application节点下配置,与activity同级;
一下贴出所有的 application 节点,方便理解

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".RandomService">
            <intent-filter>
                <action android:name="www.lyd.random"/>
            </intent-filter>
        </service>
    </application>

主要代码:

        <service android:name=".RandomService">//服务类,可以直接复制限定名
            <intent-filter>
                <action android:name="www.lyd.random"/>//对应的action
            </intent-filter>
        </service>

2. Service 的生命周期

  • onCreate: 创建服务
  • onStart: 开始服务(2.0以下,已经废弃)
  • onStartCommand 开始服务(2.0以及上使用)

1、直接启动服务
当 startService(intent);//启动service 时候
启动服务可以多次运行,但是oncreate只执行一次

服务会执行onCreate()  onStartCommand()方法,服务处于运行状态,直到自身调用stopSelf()方法或者其他组件调用stopService()方法时服务停止,最终被系统销毁。
   服务会长期的在后台运行,并且服务的状态与开启者的状态没有关系。
   

当启动服务时,会出现
在这里插入图片描述
再看一下代码

Intent intent = new Intent();//添加action意图
intent.setAction("www.lyd.random");

这个就是android5.0的兼容问题,说以要加上

intent.setPackage(MainActivity.this.getPackageName());//兼容Android 5.0

首先运行的是

@Override
    public void onCreate() {
        Log.i("onCreate","服务已经启动");//只会启动一次,在服务没停止之前,不管在启动多少次服务,只运行一次
        super.onCreate();
    }

其次

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int num = (int) (Math.random()*10);
        Log.i("random","num=" + num);
        return super.onStartCommand(intent, flags, startId);
    }

1
多次点击
2
stopService(intent);//销毁服务,未开启时不会销毁
3
2、使用绑定服务

服务会执行onCreate() -> onBind()方法,服务处于绑定状态, 客户端通过unbindService()方法关闭连接,解除绑定时,系统将直接销毁服务。
   服务与开启者的状态有关,当调用者销毁了,服务也会被销毁。

绑定服务只能运行一次,也就是绑定之后不能再继续绑定服务
这里就不粘贴代码,最后放总代码

绑定服务会先执行onCreate(),然后onBind方法,最后执行ServiceConnection的回调方法onServiceConnected;
运行绑定服务
在这里插入图片描述
发现,onServiceConnected没有输出

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("MainActivity","服务已经连接 传递信息");
        }

原因就是在onBind返回的是null,应该返回一个IBinder,在onServiceConnected可以调用。

多次运行绑定服务也只会运行一次
2
在点击启动服务,只会运行onStartCommand
在这里插入图片描述
最后解除绑定
在这里插入图片描述

3. Service 的通信

  • 在Android系统中,服务的通信方式有两种,一种是本地服务通信,一种是远程服务通信。
  • 本地服务通信是指应用程序内部的通信,而远程服务通信是指两个应用程序之间的通信。使用这两种方式进行通信时必须满足一个前提,就是服务必须以绑定方式开启

粘贴一张上课ppt内容
在这里插入图片描述

就介绍到这里,有问题请帮忙指证,谢谢!

接下来粘贴代码

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myservice">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".RandomService">
            <intent-filter>
                <action android:name="www.lyd.random"/>
            </intent-filter>
        </service>
    </application>

</manifest>

activity.layout.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"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/start_Service"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="启动服务"/>
        <Button
            android:id="@+id/stop_Service"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="停止服务"/>
        <Button
            android:id="@+id/upBind_Service"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="绑定服务"/>
        <Button
            android:id="@+id/outBind_Service"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="解绑服务"/>

    </LinearLayout>
</LinearLayout>

MainActivity.java

package com.example.myservice;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
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.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private onClickListener listener = new onClickListener();
    private ReceiveServiceMessage conn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.start_Service).setOnClickListener(listener);
        findViewById(R.id.stop_Service).setOnClickListener(listener);
        findViewById(R.id.upBind_Service).setOnClickListener(listener);
        findViewById(R.id.outBind_Service).setOnClickListener(listener);
        conn = new ReceiveServiceMessage();
    }

    class ReceiveServiceMessage implements ServiceConnection{

        //绑定回调方法
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            RandomService.myBinder Binder = (RandomService.myBinder)service;
            Log.i("MainActivity","服务已经连接 传递信息" + Binder.getNumber());

        }

        //解绑回调方法
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("MainActivity","服务断开连接");
        }
    }


    class onClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();//添加action意图
            intent.setAction("www.lyd.random");
            intent.setPackage(MainActivity.this.getPackageName());//兼容Android 5.0
            switch (v.getId()){
                case R.id.start_Service:
                    //显示意图启动Service
                    /**
                     * Intent intent = new Intent(com.example.myservice.RandomService.class);
                     * context.startService(intent);
                     **/
                    startService(intent);//启动service
                    break;
                case R.id.stop_Service:
                    stopService(intent);//销毁服务,未开启时不会销毁
                    break;
                case R.id.upBind_Service:
                    bindService(intent,conn, Service.BIND_AUTO_CREATE);//绑定服务只执行一次
                    break;
                case R.id.outBind_Service:
                    Boolean isBind = bindService(intent, conn, Context.BIND_AUTO_CREATE);
                    if(isBind){
                        unbindService(conn);
                        isBind = false;
                    }
                    break;
            }
        }
    }
}

RandomService.java

package com.example.myservice;

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

import androidx.annotation.Nullable;

public class RandomService extends Service {

    class myBinder extends Binder{
        int number;

        public int getNumber() {
            return number;
        }

        public void setNumber(int number) {
            this.number = number;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        myBinder MyBinder = new myBinder();
        int number = (int) (Math.random()*100) + 1;
        String str = "onBind通信, 随机信息:" + number;
        Log.i("onBind","绑定了服务 "+str);
        MyBinder.setNumber(number);
        return MyBinder;//要返回一个Binder,返回null,onServiceConnected就不会进行
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("onUnBind","解除绑定服务");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        Log.i("onCreate","服务已经启动");//只会启动一次,在服务没停止之前,不管在启动多少次服务,只运行一次
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int num = (int) (Math.random()*10);
        Log.i("random","num=" + num);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.i("onDestroy","服务已经销毁");
        super.onDestroy();
    }
}

谢谢大家的阅读!

👍创作不易,如有错误请指正,感谢观看!记得点赞哦!👍

相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
105 0
|
8月前
|
缓存 API 开发者
HarmonyOS学习路之开发篇——Service Ability
基于Service模板的Ability(以下简称“Service”)主要用于后台运行任务(如执行音乐播放、文件下载等),但不提供用户交互界面。Service可由其他应用或Ability启动,即使用户切换到其他应用,Service仍将在后台继续运行。
|
Android开发
深入剖析Android四大组件(六)——相对完美的后台Service实现播放音乐功能
深入剖析Android四大组件(六)——相对完美的后台Service实现播放音乐功能
196 0
harmonyOS:Service本地启动和停止的演示
为了更好的搞懂Service Ability,光看文档是没有用的,还得实操,在创建好Service ,我们就启动,来做演示,设定的代码是每启动一次,对应的值就加一
harmonyOS:Service本地启动和停止的演示
harmonyOS:Service远程设备启动和停止的演示
紧接着上篇,这次来实现远程设备的Service启动和关闭,打开两个远程设备,写好代码后,开始实操 1.先在第一个设备运行程序,然后选择始终允许
harmonyOS:Service远程设备启动和停止的演示
|
Android开发
|
Android开发
|
缓存 ARouter 安全
安卓APP改装框架 ARouter
一个用于帮助 Android App 进行组件化改造的框架 —— 支持模块间的路由、通信、解耦
|
弹性计算 缓存 应用服务中间件
一个netdisk storage backend app webos和增强的全功能网站云设想
本文关键字:利用网盘空间,network filesystem代替静态网站空间,做成静态网站的动态模块,利用v2ray,nginx给onedrive+onemanager做自动cdn,利用网盘代替函数计算
298 0
一个netdisk storage backend app webos和增强的全功能网站云设想

热门文章

最新文章