Skip to content

Commit f57db8a

Browse files
committed
返回时进入后台
1 parent 7ab5a55 commit f57db8a

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

android/app/src/main/java/top/wherewego/vnt_app/MainActivity.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Intent;
44
import android.net.VpnService;
55
import android.os.Bundle;
6+
import android.util.Log;
67

78
import androidx.annotation.NonNull;
89

@@ -27,13 +28,17 @@ public class MainActivity extends FlutterActivity {
2728
protected void onCreate(Bundle savedInstanceState) {
2829
super.onCreate(savedInstanceState);
2930
}
30-
31-
31+
@Override
32+
protected void onDestroy() {
33+
super.onDestroy();
34+
MyVpnService.stopVpn();
35+
}
3236
@Override
3337
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
3438
super.configureFlutterEngine(flutterEngine);
3539
channel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL);
3640
channel.setMethodCallHandler((call, result) -> {
41+
Log.i("moveTaskToBack","moveTaskToBack");
3742
if (call.method.equals("startVpn")) {
3843
try {
3944
Map<String, Object> arguments = call.arguments();
@@ -48,6 +53,11 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
4853
} catch (Exception e) {
4954
result.error("VPN_ERROR", "stopVpn", e);
5055
}
56+
result.success(null);
57+
}else if (call.method.equals("moveTaskToBack")) {
58+
// 将应用移至后台而不是退出应用
59+
moveTaskToBack(true);
60+
result.success(null);
5161
} else {
5262
result.notImplemented();
5363
}

lib/main.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ class VntApp extends StatelessWidget {
6565
theme: ThemeData(
6666
primarySwatch: Colors.blue,
6767
),
68-
home: const HomePage(),
68+
home: PopScope(
69+
canPop: false,
70+
onPopInvoked: (didPop) {
71+
if (didPop) {
72+
return;
73+
}
74+
VntAppCall.moveTaskToBack();
75+
},
76+
child: const HomePage()),
6977
);
7078
}
7179
}

lib/vnt/vnt_manager.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ class VntAppCall {
218218
switch (call.method) {
219219
case 'stopVnt':
220220
await vntManager.removeAll();
221+
break;
222+
case 'startVnt':
223+
break;
221224
default:
222225
throw PlatformException(
223226
code: 'Unimplemented',
@@ -232,6 +235,10 @@ class VntAppCall {
232235
.invokeMethod('startVpn', rustDeviceConfigToMap(info, mtu));
233236
}
234237

238+
static Future<void> moveTaskToBack() async {
239+
return await VntAppCall.channel.invokeMethod('moveTaskToBack');
240+
}
241+
235242
static Future<void> stopVpn() async {
236243
return await VntAppCall.channel.invokeMethod('stopVpn');
237244
}

0 commit comments

Comments
 (0)