File tree Expand file tree Collapse file tree 3 files changed +28
-3
lines changed
android/app/src/main/java/top/wherewego/vnt_app Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change 33import android .content .Intent ;
44import android .net .VpnService ;
55import android .os .Bundle ;
6+ import android .util .Log ;
67
78import 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 }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments