@linux1s1s
2019-02-14T16:02:51.000000Z
字数 6240
阅读 1895
AndroidRefine
2016-11
系列博文:
Android 生命周期 - Activity
Android 生命周期 - Service
Android 生命周期 - BroadcastReceiver
Android 生命周期 - Fragment
Android 生命周期 - View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fresco.mutex.com.lifecycle.MainActivity">
<TextView
android:id="@+id/activity_main_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:clickable="true"
android:onClick="jumpNow"
android:text="Jump Now!"
android:textSize="30dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fresco.mutex.com.lifecycle.MainActivity">
<TextView
android:id="@+id/activity_main_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:clickable="true"
android:onClick="startServer"
android:text="StartServer Now!"
android:textSize="30dp" />
</RelativeLayout>
package fresco.mutex.com.lifecycle;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final String TAG = "mutex";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "MainActivity onCreate()");
}
@Override
protected void onDestroy() {
Log.i(TAG, "MainActivity onDestroy()");
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "MainActivity onStart()");
}
@Override
protected void onStop() {
Log.i(TAG, "MainActivity onStop()");
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "MainActivity onResume()");
}
@Override
protected void onPause() {
Log.i(TAG, "MainActivity onPause()");
super.onPause();
}
public void jumpNow(View view) {
startActivity(new Intent(this, SecondActivity.class));
}
}
package fresco.mutex.com.lifecycle;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.i(MainActivity.TAG, "SecondActivity onCreate()");
}
@Override
protected void onDestroy() {
Log.i(MainActivity.TAG, "SecondActivity onDestroy()");
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
Log.i(MainActivity.TAG, "SecondActivity onStart()");
}
@Override
protected void onStop() {
Log.i(MainActivity.TAG, "SecondActivity onStop()");
stopService(new Intent(this, BCService.class));
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
Log.i(MainActivity.TAG, "SecondActivity onResume()");
}
@Override
protected void onPause() {
Log.i(MainActivity.TAG, "SecondActivity onPause()");
super.onPause();
}
public void startServer(View view) {
startService(new Intent(this, BCService.class));
}
}
package fresco.mutex.com.lifecycle;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
public class BCService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.i(MainActivity.TAG, "BCService onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(MainActivity.TAG, "BCService onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(MainActivity.TAG, "BCService onBind");
return null;
}
@Override
public void onDestroy() {
Log.i(MainActivity.TAG, "BCService onDestroy");
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fresco.mutex.com.lifecycle">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.DELETE" />
</intent-filter>
</activity>
<service android:name=".BCService" />
</application>
</manifest>
以上周期流程图可以分三种情况分析:
问题:onUnbind()回调返回不同boolean会有什么不同的影响?
为了验证以上结论加入以下代码:
@Override
public boolean onUnbind(Intent intent) {
//Default is false
Log.i(MainActivity.TAG, "BCService onUnbind");
return true;
}
@Override
public void onRebind(Intent intent) {
Log.i(MainActivity.TAG, "BCService onRebind");
super.onRebind(intent);
}
然后如下操作:
- 点击调用 startService
- 点击调用 bindService
- 点击调用 unbindService
- 点击调用 bindService
- 点击调用 unbindService
- 点击调用 stopService
Log日志如下:
接下来我们将Java代码修改onUnbind()方法的返回值为false,依然保持上面的操作不变,Log日志如下: