服务概述

image.png
image.png

服务的生命周期

image.png

服务的两种启动方式-启动方式(startService)

image.png
image.png

MainActivity.java

  1. package com.bluelesson.servicedemo;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. public class MainActivity extends AppCompatActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. }
  12. public void btnstart1(View view) {
  13. //1.创建Intent
  14. Intent intent=new Intent();
  15. //2.设置信息
  16. intent.setClass(this,MyService.class);
  17. // 3.启动服务
  18. startService(intent);
  19. }
  20. public void btnstop1(View view) {
  21. //1.创建Intent
  22. Intent intent=new Intent();
  23. //2.设置信息
  24. intent.setClass(this,MyService.class);
  25. // 3.关闭服务
  26. stopService(intent);
  27. }
  28. }

MyService.java

  1. package com.bluelesson.servicedemo;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.util.Log;
  6. public class MyService extends Service {
  7. public MyService() {
  8. }
  9. @Override
  10. public IBinder onBind(Intent intent) {
  11. // TODO: Return the communication channel to the service.
  12. throw new UnsupportedOperationException("Not yet implemented");
  13. }
  14. @Override
  15. public void onCreate() {
  16. super.onCreate();
  17. Log.d("15pb","MyService::onCreate");
  18. }
  19. @Override
  20. public int onStartCommand(Intent intent, int flags, int startId) {
  21. Log.d("15pb","MyService::onStartCommand");
  22. return super.onStartCommand(intent, flags, startId);
  23. }
  24. @Override
  25. public void onDestroy() {
  26. Log.d("15pb","MyService::onDestroy");
  27. super.onDestroy();
  28. }
  29. }

startService方式实例-电话监听器

image.png

关键代码

image.png
image.png
image.png
**

AndroidManifest.xml

image.png

activity_main.xml

image.png

MainActivity.java

image.png

MyService.java

  1. package com.bluelesson.servicedemo;
  2. import android.app.Service;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.IBinder;
  6. import android.telephony.PhoneStateListener;
  7. import android.telephony.TelephonyManager;
  8. import android.util.Log;
  9. public class MyService extends Service {
  10. public MyService() {
  11. }
  12. @Override
  13. public IBinder onBind(Intent intent) {
  14. // TODO: Return the communication channel to the service.
  15. throw new UnsupportedOperationException("Not yet implemented");
  16. }
  17. public class MyPhoneListener extends PhoneStateListener{
  18. @Override
  19. public void onCallStateChanged(int state, String incomingNumber) {
  20. super.onCallStateChanged(state, incomingNumber);
  21. switch (state){
  22. case TelephonyManager.CALL_STATE_IDLE:
  23. Log.d("15pb","MyService::电话空闲");
  24. break;
  25. case TelephonyManager.CALL_STATE_OFFHOOK:
  26. Log.d("15pb","MyService::电话接听");
  27. break;
  28. case TelephonyManager.CALL_STATE_RINGING:
  29. Log.d("15pb","MyService::电话响铃");
  30. break;
  31. }
  32. }
  33. }
  34. private TelephonyManager mTm;
  35. private MyPhoneListener mListener;
  36. @Override
  37. public void onCreate() {
  38. super.onCreate();
  39. Log.d("15pb","MyService::onCreate");
  40. //初始化
  41. //1.获取电话管理器
  42. mTm=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  43. //2.创建监听器
  44. mListener=new MyPhoneListener();
  45. //3.益听
  46. mTm.listen(mListener,PhoneStateListener.LISTEN_CALL_STATE);
  47. }
  48. @Override
  49. public int onStartCommand(Intent intent, int flags, int startId) {
  50. Log.d("15pb","MyService::onStartCommand");
  51. return super.onStartCommand(intent, flags, startId);
  52. }
  53. @Override
  54. public void onDestroy() {
  55. Log.d("15pb","MyService::onDestroy");
  56. super.onDestroy();
  57. //取消监听
  58. mTm.listen(mListener,PhoneStateListener.LISTEN_NONE);
  59. }
  60. }

startService方式实例-计时器

image.png

关键代码

image.png
image.png
image.png
image.png
image.png
image.png

activity_main.xml

image.png

MainActivity.java

image.png

TimerService.java

  1. package com.bluelesson.servicedemo;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.util.Log;
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8. public class TimerService extends Service {
  9. public TimerService() {
  10. }
  11. @Override
  12. public IBinder onBind(Intent intent) {
  13. // TODO: Return the communication channel to the service.
  14. throw new UnsupportedOperationException("Not yet implemented");
  15. }
  16. private Timer mTimer;
  17. private int mCount=0;
  18. private String mShowText="当前计时:0";
  19. @Override
  20. public void onCreate() {
  21. super.onCreate();
  22. //1.创建定时器
  23. mTimer=new Timer();
  24. //2.创建定时器任务
  25. TimerTask timerTask = new TimerTask() {
  26. @Override
  27. public void run() {
  28. Log.d("15pb",mShowText+mCount);
  29. //发送广播
  30. Intent intent=new Intent();
  31. intent.setAction("MyTimer");
  32. intent.putExtra("time",mCount);
  33. sendBroadcast(intent);
  34. mCount++;
  35. }
  36. };
  37. //3.给定时器指定定时器任务
  38. mTimer.schedule(timerTask,0,1000);
  39. }
  40. @Override
  41. public int onStartCommand(Intent intent, int flags, int startId) {
  42. mCount=0;//重置
  43. return super.onStartCommand(intent, flags, startId);
  44. }
  45. @Override
  46. public void onDestroy() {
  47. super.onDestroy();
  48. //取消定时器
  49. if(mTimer!=null)
  50. mTimer.cancel();
  51. mTimer=null;
  52. }
  53. }


服务的两种启动方式-绑定方式(bindService)

image.png
image.png
image.png

绑定方式(bindService)实例-本地人员查询系统

实例

BindService.zip
image.png

关键代码

image.png
image.png
image.png
image.png
image.png

image.png
image.png
image.png

main_activity.xml

image.png

IPersonListener.java

image.png

MyBindService.java

  1. package com.bluelesson.bindservice;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.Binder;
  5. import android.os.IBinder;
  6. import android.util.Log;
  7. public class MyBindService extends Service {
  8. public MyBindService() {
  9. }
  10. String strings[] = {"齐天大圣","猪八戒","沙和尚","白龙马"};
  11. public class MyBinder extends Binder implements IPersonListener{
  12. @Override
  13. public String queryNameById(int id) {
  14. if(id<0|| id>3) {
  15. return "唐 僧";
  16. }
  17. return strings[id];
  18. }
  19. }
  20. public MyBinder myBinder;
  21. @Override
  22. public void onCreate() {
  23. super.onCreate();
  24. myBinder = new MyBinder();
  25. Log.d("15pb","MyBindService::onCreate");
  26. }
  27. @Override
  28. public IBinder onBind(Intent intent) {
  29. // TODO: Return the communication channel to the service.
  30. Log.d("15pb","MyBindService::onBind");
  31. return myBinder;
  32. }
  33. @Override
  34. public boolean onUnbind(Intent intent) {
  35. Log.d("15pb","MyBindService::onUnbind");
  36. if(myBinder!=null){
  37. myBinder=null;
  38. }
  39. return super.onUnbind(intent);
  40. }
  41. @Override
  42. public void onDestroy() {
  43. super.onDestroy();
  44. }
  45. }

MainActivity.java

  1. package com.bluelesson.bindservice;
  2. import android.content.ComponentName;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.os.IBinder;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. public class MainActivity extends AppCompatActivity {
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. myServiceConnection = new MyServiceConnection();
  18. }
  19. private MyServiceConnection myServiceConnection;
  20. private MyBindService.MyBinder myBinder;
  21. public class MyServiceConnection implements ServiceConnection{
  22. @Override
  23. public void onServiceConnected(ComponentName componentName,
  24. IBinder iBinder) {
  25. myBinder = (MyBindService.MyBinder)iBinder;
  26. }
  27. @Override
  28. public void onServiceDisconnected(ComponentName componentName) {
  29. }
  30. }
  31. public void btnBind(View view) {
  32. Intent intent=new Intent(this, MyBindService.class);
  33. bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);
  34. }
  35. public void btnUnBind(View view) {
  36. Intent intent=new Intent(this, MyBindService.class);
  37. unbindService(myServiceConnection);
  38. stopService(intent);
  39. }
  40. public void btnQuery(View view) {
  41. EditText editText = findViewById(R.id.edit_query);
  42. String string = editText.getText().toString();
  43. int num = Integer.parseInt(string);
  44. if(myBinder!=null) {
  45. String name = myBinder.queryNameById(num);
  46. TextView textView = findViewById(R.id.tv1);
  47. textView.setText(name);
  48. }
  49. }
  50. }

绑定方式(bindService)实例-远程人员查询系统

image.png
image.png

关键代码

image.png
image.png
image.png
image.png

AIDL的app

image.png

MyBindService.java

image.png

AndroidManifest.xml

image.png

TestBindService.app(注意要把aidl文件夹复制过来)

activity_main.xml

image.png

Activity.java(注意)

  1. package com.bluelesson.testbindservice;
  2. import android.content.ComponentName;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.os.IBinder;
  7. import android.os.RemoteException;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import com.bluelesson.bindservice.IPersonListenerAIDL;
  15. public class MainActivity extends AppCompatActivity {
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. myServiceConnection = new MyServiceConnection();
  21. }
  22. private MyServiceConnection myServiceConnection;
  23. // private MyBindService.MyBinder myBinder;
  24. IPersonListenerAIDL aidl;
  25. public class MyServiceConnection implements ServiceConnection {
  26. @Override
  27. public void onServiceConnected(ComponentName componentName,
  28. IBinder iBinder) {
  29. // myBinder = (MyBindService.MyBinder)iBinder;
  30. Log.d("15pb", "MyServiceConnection:: onServiceConnected");
  31. aidl = IPersonListenerAIDL.Stub.asInterface(iBinder);
  32. }
  33. @Override
  34. public void onServiceDisconnected(ComponentName componentName) {
  35. }
  36. }
  37. public void btnBind(View view) {
  38. Intent intent=new Intent("MyBindService");
  39. bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);
  40. }
  41. public void btnUnBind(View view) {
  42. Intent intent=new Intent("MyBindService");
  43. unbindService(myServiceConnection);
  44. stopService(intent);
  45. }
  46. public void btnQuery(View view) {
  47. EditText editText = findViewById(R.id.edit_query);
  48. String string = editText.getText().toString();
  49. int num = Integer.parseInt(string);
  50. if(aidl!=null) {
  51. String name = null;
  52. try {
  53. name = aidl.queryNameById(num);
  54. } catch (RemoteException e) {
  55. e.printStackTrace();
  56. }
  57. TextView textView = findViewById(R.id.tv1);
  58. textView.setText(name);
  59. }
  60. }
  61. }

绑定方式(bindService)实例-远程人员查询系统(升级版)

服务的两种启动方式-双剑合璧