开发中经常遇到app返回不要退出程序,而是回到桌面即可
flutter端代码:
_backDeskTop() async {
//初始化通信管道-设置退出到手机桌面
String CHANNEL = "android/back/desktop";
final platform = MethodChannel(CHANNEL);
//通知安卓返回,到手机桌面
try {
final bool out = await platform.invokeMethod('backDesktop');
if (out) debugPrint('返回到桌面');
} on PlatformException catch (e) {
debugPrint("通信失败(设置回退到安卓手机桌面:设置失败)");
print(e.toString());
}
return Future.value(false);
}
Java代码:
//通讯名称,回到手机桌面
private final String CHANNEL = "android/back/desktop";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
(methodCall, result) -> {
if (methodCall.method.equals("backDesktop")) {
result.success(true);
moveTaskToBack(false);
}
}
);
}
image
更多详解:
喜欢可以加@群号:913934649
简书: https://www.jianshu.com/u/88db5f15770d
网友评论