Flutter 跳转到系统设置等界面实现
MainActivity.java
导入:
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import android.content.Intent;
import android.os.Bundle;
增加通道名称变量:
private static final String CHANNEL = "toSetting";
增加onCreate方法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if(call.method.equals("to")){
Intent intent = new Intent();
intent.setAction(call.arguments.toString());
startActivity(intent);
}else{
result.notImplemented();
}
}
}
);
}
完整代码示例:
package com.example.chat;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "ToSetting";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if(call.method.equals("to")){
Intent intent = new Intent();
intent.setAction(call.arguments.toString());
startActivity(intent);
}else{
result.notImplemented();
}
}
}
);
}
}
Flutter中使用:
static const platform = const MethodChannel('ToSetting');
platform.invokeMethod('to','android.settings.MEMORY_CARD_SETTINGS');
版权申明
本文系作者 @Mr.Yang 原创发布在果皮皮站点。未经许可,禁止转载。
暂无评论数据