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');
分类: Flutter 标签: flutterIntent

评论

暂无评论数据

暂无评论数据

目录