[关闭]
@linux1s1s 2019-02-14T16:02:51.000000Z 字数 6240 阅读 1894

Android 生命周期 - Service

AndroidRefine 2016-11


系列博文:

Android 生命周期 - Activity
Android 生命周期 - Service
Android 生命周期 - BroadcastReceiver
Android 生命周期 - Fragment
Android 生命周期 - View

资源文件

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context="fresco.mutex.com.lifecycle.MainActivity">
  12. <TextView
  13. android:id="@+id/activity_main_jump"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_centerInParent="true"
  17. android:clickable="true"
  18. android:onClick="jumpNow"
  19. android:text="Jump Now!"
  20. android:textSize="30dp" />
  21. </RelativeLayout>

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context="fresco.mutex.com.lifecycle.MainActivity">
  12. <TextView
  13. android:id="@+id/activity_main_jump"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_centerInParent="true"
  17. android:clickable="true"
  18. android:onClick="startServer"
  19. android:text="StartServer Now!"
  20. android:textSize="30dp" />
  21. </RelativeLayout>

Java代码

MainActivity

  1. package fresco.mutex.com.lifecycle;
  2. import android.content.Intent;
  3. import android.os.PersistableBundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. public class MainActivity extends AppCompatActivity {
  9. public static final String TAG = "mutex";
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. Log.i(TAG, "MainActivity onCreate()");
  15. }
  16. @Override
  17. protected void onDestroy() {
  18. Log.i(TAG, "MainActivity onDestroy()");
  19. super.onDestroy();
  20. }
  21. @Override
  22. protected void onStart() {
  23. super.onStart();
  24. Log.i(TAG, "MainActivity onStart()");
  25. }
  26. @Override
  27. protected void onStop() {
  28. Log.i(TAG, "MainActivity onStop()");
  29. super.onStop();
  30. }
  31. @Override
  32. protected void onResume() {
  33. super.onResume();
  34. Log.i(TAG, "MainActivity onResume()");
  35. }
  36. @Override
  37. protected void onPause() {
  38. Log.i(TAG, "MainActivity onPause()");
  39. super.onPause();
  40. }
  41. public void jumpNow(View view) {
  42. startActivity(new Intent(this, SecondActivity.class));
  43. }
  44. }

SecondActivity.java

  1. package fresco.mutex.com.lifecycle;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.util.Log;
  6. import android.view.View;
  7. public class SecondActivity extends AppCompatActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_second);
  12. Log.i(MainActivity.TAG, "SecondActivity onCreate()");
  13. }
  14. @Override
  15. protected void onDestroy() {
  16. Log.i(MainActivity.TAG, "SecondActivity onDestroy()");
  17. super.onDestroy();
  18. }
  19. @Override
  20. protected void onStart() {
  21. super.onStart();
  22. Log.i(MainActivity.TAG, "SecondActivity onStart()");
  23. }
  24. @Override
  25. protected void onStop() {
  26. Log.i(MainActivity.TAG, "SecondActivity onStop()");
  27. stopService(new Intent(this, BCService.class));
  28. super.onStop();
  29. }
  30. @Override
  31. protected void onResume() {
  32. super.onResume();
  33. Log.i(MainActivity.TAG, "SecondActivity onResume()");
  34. }
  35. @Override
  36. protected void onPause() {
  37. Log.i(MainActivity.TAG, "SecondActivity onPause()");
  38. super.onPause();
  39. }
  40. public void startServer(View view) {
  41. startService(new Intent(this, BCService.class));
  42. }
  43. }

BCService.java

  1. package fresco.mutex.com.lifecycle;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.support.annotation.Nullable;
  6. import android.util.Log;
  7. public class BCService extends Service {
  8. @Override
  9. public void onCreate() {
  10. super.onCreate();
  11. Log.i(MainActivity.TAG, "BCService onCreate");
  12. }
  13. @Override
  14. public int onStartCommand(Intent intent, int flags, int startId) {
  15. Log.i(MainActivity.TAG, "BCService onStartCommand");
  16. return super.onStartCommand(intent, flags, startId);
  17. }
  18. @Nullable
  19. @Override
  20. public IBinder onBind(Intent intent) {
  21. Log.i(MainActivity.TAG, "BCService onBind");
  22. return null;
  23. }
  24. @Override
  25. public void onDestroy() {
  26. Log.i(MainActivity.TAG, "BCService onDestroy");
  27. super.onDestroy();
  28. }
  29. }

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="fresco.mutex.com.lifecycle">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".MainActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. <activity android:name=".SecondActivity">
  17. <intent-filter>
  18. <action android:name="android.intent.action.DELETE" />
  19. </intent-filter>
  20. </activity>
  21. <service android:name=".BCService" />
  22. </application>
  23. </manifest>

实验结果(Log日志)

此处输入图片的描述
此处输入图片的描述

Server生命周期

此处输入图片的描述

以上周期流程图可以分三种情况分析:

startService / stopService

bindService / unbindService

startService&&bindService / unbindService&&stopService(交错情况)

问题:onUnbind()回调返回不同boolean会有什么不同的影响?
此处输入图片的描述

为了验证以上结论加入以下代码:

  1. @Override
  2. public boolean onUnbind(Intent intent) {
  3. //Default is false
  4. Log.i(MainActivity.TAG, "BCService onUnbind");
  5. return true;
  6. }
  7. @Override
  8. public void onRebind(Intent intent) {
  9. Log.i(MainActivity.TAG, "BCService onRebind");
  10. super.onRebind(intent);
  11. }

然后如下操作:

  • 点击调用 startService
  • 点击调用 bindService
  • 点击调用 unbindService
  • 点击调用 bindService
  • 点击调用 unbindService
  • 点击调用 stopService

Log日志如下:
此处输入图片的描述

接下来我们将Java代码修改onUnbind()方法的返回值为false,依然保持上面的操作不变,Log日志如下:
此处输入图片的描述

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注