Skip to content

Commit 61f0c1b

Browse files
author
mj
committed
IPC
1 parent 229a170 commit 61f0c1b

File tree

8 files changed

+123
-60
lines changed

8 files changed

+123
-60
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
<activity android:name=".ui.activity.butter_knife.TestButterKnifeActivity" />
4848
<!--为插件占坑的代理Activity-->
4949
<activity android:name=".module.plugins.PluginProxyActivity" />
50+
<!--服务进程-->
51+
<service
52+
android:name=".module.process.BackgroundService"
53+
android:process=":background_service" />
5054
</application>
5155

5256
</manifest>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.mj.android_note.module.process;
2+
3+
import android.app.Service;
4+
import android.content.Intent;
5+
import android.os.Binder;
6+
import android.os.IBinder;
7+
8+
import androidx.annotation.Nullable;
9+
10+
import com.mj.lib.base.log.LogUtil;
11+
12+
/**
13+
* Author : MJ
14+
* Date : 2020-03-19--21:23
15+
16+
* Description : 后台服务
17+
*/
18+
public class BackgroundService extends Service {
19+
20+
21+
private static final String TAG = "BackgroundService";
22+
private SerViceBinder serViceBinder = new SerViceBinder();
23+
24+
// binder 服务
25+
class SerViceBinder extends Binder {
26+
BackgroundService getService() {
27+
return BackgroundService.this;
28+
}
29+
}
30+
31+
@Override
32+
public void onCreate() {
33+
super.onCreate();
34+
LogUtil.e(ProcessConstants.TAG, " ---onCreate");
35+
}
36+
37+
@Nullable
38+
@Override
39+
public IBinder onBind(Intent intent) {
40+
LogUtil.e(ProcessConstants.TAG, "onBind : intent = " + intent);
41+
return serViceBinder;
42+
}
43+
44+
@Override
45+
public int onStartCommand(Intent intent, int flags, int startId) {
46+
LogUtil.e(ProcessConstants.TAG, "onStartCommand intent: " + intent + "--flags = " + flags + "--startId:" + startId);
47+
return START_STICKY_COMPATIBILITY;
48+
}
49+
50+
@Override
51+
public boolean onUnbind(Intent intent) {
52+
LogUtil.e(ProcessConstants.TAG, "onUnbind intent:" + intent);
53+
return super.onUnbind(intent);
54+
}
55+
56+
@Override
57+
public void onDestroy() {
58+
super.onDestroy();
59+
LogUtil.e(ProcessConstants.TAG, "onDestroy");
60+
}
61+
62+
63+
public int add(int i, int j) {
64+
return i + j;
65+
}
66+
67+
}

app/src/main/java/com/mj/android_note/module/process/FirstProcessActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public void onClick(View v) {
4040
@Override
4141
protected void onResume() {
4242
super.onResume();
43-
tvText.setText(String.valueOf(MainProcessActivity.TEST_CONSTANT_FROM_MULTI_PROCESS_DEFAULT));
4443
}
4544

4645
}

app/src/main/java/com/mj/android_note/module/process/MainProcessActivity.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.mj.android_note.module.process;
22

3+
import android.annotation.SuppressLint;
4+
import android.content.ComponentName;
35
import android.content.Context;
6+
import android.content.Intent;
7+
import android.content.ServiceConnection;
48
import android.os.Bundle;
9+
510
import androidx.annotation.Nullable;
11+
12+
import android.os.IBinder;
613
import android.view.View;
714
import android.widget.TextView;
815

916
import com.mj.android_note.R;
1017
import com.mj.android_note.base.BaseActivity;
18+
import com.mj.lib.base.log.LogUtil;
1119

1220
/**
1321
* Author : MJ
@@ -30,28 +38,47 @@ public static void launcher(Context context) {
3038

3139
private TextView tvText;
3240

41+
private BackgroundService backgroundService;
42+
3343
@Override
3444
protected void onCreate(@Nullable Bundle savedInstanceState) {
3545
super.onCreate(savedInstanceState);
3646
setContentView(R.layout.activity_main_process);
3747
tvText = findViewById(R.id.activity_main_process_text);
3848
findViewById(R.id.activity_main_process_btn).setOnClickListener(new View.OnClickListener() {
49+
@SuppressLint("SetTextI18n")
3950
@Override
4051
public void onClick(View v) {
41-
FirstProcessActivity.launcher(MainProcessActivity.this);
52+
// FirstProcessActivity.launcher(MainProcessActivity.this);
53+
Intent intent = new Intent(MainProcessActivity.this, BackgroundService.class);
54+
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
55+
// int add = backgroundService.add(10, 8);
56+
// LogUtil.e(ProcessConstants.TAG, "result = " + add);
57+
// tvText.setText("结果为:" + add);
4258
}
4359
});
4460
}
4561

62+
/**
63+
* service Connection
64+
*/
65+
private ServiceConnection serviceConnection = new ServiceConnection() {
66+
@Override
67+
public void onServiceConnected(ComponentName name, IBinder service) {
68+
// backgroundService = ((BackgroundService.SerViceBinder) service).getService();
69+
LogUtil.e(ProcessConstants.TAG, "onServiceConnected," + name + "--service:" + service.getClass().getName());
70+
}
71+
72+
@Override
73+
public void onServiceDisconnected(ComponentName name) {
74+
LogUtil.e(ProcessConstants.TAG, "onServiceDisconnected,name:" + name);
75+
}
76+
};
77+
78+
4679
@Override
4780
protected void onResume() {
4881
super.onResume();
49-
tvText.setText(String.valueOf(TEST_CONSTANT_FROM_MULTI_PROCESS_DEFAULT));
5082
}
5183

52-
@Override
53-
public void onDetachedFromWindow() {
54-
super.onDetachedFromWindow();
55-
TEST_CONSTANT_FROM_MULTI_PROCESS_DEFAULT = 100;
56-
}
5784
}

app/src/main/java/com/mj/android_note/module/process/NoteProvider.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323

2424
public class NoteProvider extends ContentProvider {
2525
private static final String TAG = "NoteProvider";
26-
public static int TEST_CONSTANT_FROM_MULTI_PROCESS_DEFAULT = 100;
2726

2827
@Override
2928
public boolean onCreate() {
30-
Log.e(TAG, TAG + " onCreate() processName=" + getProcessName());
3129
return true;
3230
}
3331

@@ -59,33 +57,4 @@ public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String
5957
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
6058
return 0;
6159
}
62-
63-
String getProcessName() {
64-
String processName = "default";
65-
66-
File cmdFile = new File("/proc/" + android.os.Process.myPid() + "/cmdline");
67-
68-
if (cmdFile.exists() && !cmdFile.isDirectory()) {
69-
BufferedReader reader = null;
70-
try {
71-
reader = new BufferedReader(new InputStreamReader(new FileInputStream(cmdFile)));
72-
String procName = reader.readLine();
73-
74-
// if (!TextUtils.isEmpty(procName) && getContext() != null) {
75-
// processName = procName.trim().replace(getContext().getPackageName(), "").replace(":", "");
76-
// }
77-
return procName.trim();
78-
} catch (Exception ignored) {
79-
} finally {
80-
if (reader != null) {
81-
try {
82-
reader.close();
83-
} catch (Exception ignored) {
84-
}
85-
}
86-
}
87-
}
88-
return processName;
89-
}
90-
9160
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.mj.android_note.module.process;
2+
3+
/**
4+
* Author : MJ
5+
* Date : 2020-03-19--22:48
6+
7+
* Description :
8+
*/
9+
public class ProcessConstants {
10+
public static final String TAG = "ProcessConstants";
11+
}

app/src/main/java/com/mj/android_note/module/process/SecondProcessActivity.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818

1919
public class SecondProcessActivity extends BaseActivity {
2020

21-
/**
22-
* 测试多进程中的常量,修改后的值
23-
*/
24-
public static int TEST_CONSTANT_FROM_MULTI_PROCESS_RESULT = 200;
25-
2621
public static void launcher(Context context) {
2722
startAct(context, SecondProcessActivity.class);
2823
}
@@ -44,26 +39,18 @@ public void onClick(View v) {
4439

4540
private void updateConstantsAndProcessSync() {
4641
errorUpdateMethod();
47-
setText();
4842
}
4943

5044
/**
5145
* 错误的修改方式
5246
*/
5347
private void errorUpdateMethod() {
54-
MainProcessActivity.TEST_CONSTANT_FROM_MULTI_PROCESS_DEFAULT = TEST_CONSTANT_FROM_MULTI_PROCESS_RESULT;
5548
}
5649

5750

5851
@Override
5952
protected void onResume() {
6053
super.onResume();
61-
setText();
6254
}
6355

64-
private void setText() {
65-
tvText.setText(String.valueOf(MainProcessActivity.TEST_CONSTANT_FROM_MULTI_PROCESS_DEFAULT));
66-
}
67-
68-
6956
}

app/src/main/java/com/mj/android_note/ui/activity/MainActivity.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@
2424
import com.mj.android_note.R;
2525
import com.mj.android_note.app.NoteApplication;
2626
import com.mj.android_note.base.AndroidKeyWatcher;
27-
import com.mj.android_note.base.serializable_parcelable.School;
2827
import com.mj.android_note.base.serializable_parcelable.TestSerializableAndParcelable;
2928
import com.mj.android_note.base.serializable_parcelable.User;
3029
import com.mj.android_note.module.plugins.HookManager;
3130
import com.mj.android_note.module.plugins.PluginManager;
3231
import com.mj.android_note.module.plugins.ResourceManager;
33-
import com.mj.android_note.ui.activity.butter_knife.TestButterKnifeActivity;
34-
import com.mj.android_note.utils.ToastUtils;
32+
import com.mj.android_note.module.process.MainProcessActivity;
3533
import com.mj.lib.base.log.LogUtil;
3634
import com.mj.lib.base.thread.ThreadUtils;
3735

@@ -79,11 +77,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
7977
public void onClick(View v) {
8078
// Intent intent = new Intent(MainActivity.this, DbMainActivity.class);
8179
// startActivity(intent);
82-
Intent intent = new Intent(MainActivity.this, TestButterKnifeActivity.class);
83-
Bundle bundle = new Bundle();
84-
bundle.putParcelable("parcelable_test", new School(100, "北京大学", "北京市海淀区"));
85-
intent.putExtras(bundle);
86-
startActivity(intent);
80+
// Intent intent = new Intent(MainActivity.this, TestButterKnifeActivity.class);
81+
// Bundle bundle = new Bundle();
82+
// bundle.putParcelable("parcelable_test", new School(100, "北京大学", "北京市海淀区"));
83+
// intent.putExtras(bundle);
84+
// startActivity(intent);
85+
MainProcessActivity.launcher(MainActivity.this);
8786
}
8887
});
8988

0 commit comments

Comments
 (0)