压缩包

test2.zip

一、认识Service

服务在后台默默的运行,是不可见的

二、startService和bindService的生命周期图

1.startService:服务由自身或客户端停止

2.bindService:通过调用unbindService()来解除所有客户端绑定

image.png

三、startService与生命周期

1.布局activity_main3.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".Main3Activity"
  8. android:orientation="vertical">
  9. <Button
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:onClick="startService"
  13. android:text="STARTSERVICE"
  14. android:textSize="30sp"
  15. tools:ignore="OnClick" />
  16. <Button
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:onClick="stopService"
  20. android:text="STOPSERVICE"
  21. android:textSize="30sp"
  22. tools:ignore="OnClick" />
  23. </LinearLayout>

2.MyService服务类文件代码

  1. package com.example.test;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.util.Log;
  6. import androidx.annotation.Nullable;
  7. public class MyService extends Service {
  8. private final static String TAG=MyService.class.getSimpleName();
  9. @Override
  10. public void onCreate() {
  11. super.onCreate();
  12. Log.d(TAG,"onCreate()...");
  13. }
  14. @Override
  15. public void onStart(Intent intent, int startId) {
  16. super.onStart(intent, startId);
  17. Log.d(TAG,"onStart()...");
  18. }
  19. @Override
  20. public int onStartCommand(Intent intent, int flags, int startId) {
  21. Log.d(TAG,"onStartCommand()...");
  22. return super.onStartCommand(intent, flags, startId);
  23. }
  24. @Override
  25. public void onDestroy() {
  26. Log.d(TAG,"onDestroy()...");
  27. super.onDestroy();
  28. }
  29. @Nullable
  30. @Override
  31. public IBinder onBind(Intent intent) {
  32. Log.d(TAG,"onBind()...");
  33. return null;
  34. }
  35. // @Override
  36. // public boolean onUnbind(Intent intent) {
  37. // Log.d(TAG,"onUnbind()...");
  38. // return super.onUnbind(intent);
  39. // }
  40. }

3.Main3Activity文件代码

  1. package com.example.test;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. public class Main3Activity extends AppCompatActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main3);
  11. }
  12. public void startActivity(View view) {
  13. Intent intent = new Intent();
  14. intent.setClass(Main3Activity.this,MActivity.class);
  15. startActivity(intent);
  16. }
  17. //启动服务
  18. public void startService(View view) {
  19. startService(new Intent(this,MyService.class));
  20. }
  21. //停止服务
  22. public void stopService(View view) {
  23. stopService(new Intent(this,MyService.class));
  24. }
  25. }

4.清单文件中注册服务类组件权限

image.png

5.执行代码,点击启动服务按钮

image.png

6.点击停止服务按钮

image.png

四、bindService与生命周期

1.bind和unbind按钮的实现

image.png

2.构建绑定个解绑的桥梁

image.png

3.布局activity_main3.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".Main3Activity"
  8. android:orientation="vertical">
  9. <Button
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:onClick="startService"
  13. android:text="STARTSERVICE"
  14. android:textSize="30sp" />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:onClick="stopService"
  19. android:text="STOPSERVICE"
  20. android:textSize="30sp" />
  21. <Button
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:onClick="bindService"
  25. android:text="BINDSERVICE"
  26. android:textSize="30sp"
  27. android:layout_marginTop="100dp"/>
  28. <Button
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:onClick="unBindService"
  32. android:text="UNBINDSERVICE"
  33. android:textSize="30sp" />
  34. </LinearLayout>

4.Main3Activity文件代码

  1. package com.example.test;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.view.View;
  10. public class Main3Activity extends AppCompatActivity {
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main3);
  15. }
  16. public void startActivity(View view) {
  17. Intent intent = new Intent();
  18. intent.setClass(Main3Activity.this,MActivity.class);
  19. startActivity(intent);
  20. }
  21. //启动服务
  22. public void startService(View view) {
  23. startService(new Intent(this,MyService.class));
  24. }
  25. //停止服务
  26. public void stopService(View view) {
  27. stopService(new Intent(this,MyService.class));
  28. }
  29. public void bindService(View view) {
  30. bindService(new Intent(this,MyService.class),connecton, Context.BIND_AUTO_CREATE);
  31. }
  32. public void unBindService(View view) {
  33. unbindService(connecton);
  34. }
  35. //MainActivity与MyService的桥梁
  36. private ServiceConnection connecton=new ServiceConnection() {
  37. @Override
  38. public void onServiceConnected(ComponentName name, IBinder service) {
  39. }
  40. @Override
  41. public void onServiceDisconnected(ComponentName name) {
  42. }
  43. };
  44. //一般的写法,当此Activity被销毁的时候,自动解雇服务
  45. @Override
  46. protected void onDestroy() {
  47. super.onDestroy();
  48. unbindService(connecton);
  49. }
  50. }

5.绑定之后实现的方法(onCreate()->onBind())

image.png

6.解绑之后执行的方法(onUnbind()->onDestroy())

image.png

7.返回之后销毁服务(onUnbind()->onDestroy())

image.png