如何从方法通道检索字符串列表

时间:2018-09-22 11:00:11

标签: android flutter

我想从本地Android检索String列表,以通过Method Channel颤动。此字符串列表是所有联系电话号码。我当前的代码:

 new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
        new MethodChannel.MethodCallHandler() {
          @Override
          public void onMethodCall(MethodCall call, MethodChannel.Result result) {
            if (call.method.equals("getContacts")) {
              contacts = getContactList();

              if (contacts != null) {
                result.success(contacts);
              } else {
                result.error("UNAVAILABLE", "not avilable", null);
              }
            } else {
              result.notImplemented();
            }
          }
        });

在Flutter中:

final Iterable result = await platform.invokeMethod('getContacts');
  contactNumber = result.toList();

但是我没有得到任何回应。如何仅将电话号码从本机android检索到来电?

3 个答案:

答案 0 :(得分:1)

这是我的做法。

Android本机代码(带有字符串的发送列表):

new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodCallHandler() {
                @Override
                public void onMethodCall(MethodCall call, Result result) {
                    if (call.method.equals("samples.flutter.io/contact")) {
                        final List<String> list = new ArrayList<>();
                        list.add("Phone number 1");
                        list.add("Phone number 2");
                        list.add("Phone number 3");

                        result.success(list);
                    } else {
                        result.notImplemented();
                    }
                }
            }
    );

颤振代码:

List<dynamic> phoneNumbersList = <dynamic>[];

Future<List<String>> _getList() async {
   phoneNumbersList = await methodChannel.invokeMethod('samples.flutter.io/contact');
   print(phoneNumberList[0]);
   return phoneNumberList;
}

答案 1 :(得分:0)

我用另一种简单的方法解决了我的问题。也许会对其他人有帮助。

本地代码:

package com.y34h1a.test;
import android.database.Cursor;
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.provider.ContactsContract;

public class MainActivity extends FlutterActivity {

  private static final String CHANNEL = "samples.flutter.io/contact";
  String phoneNumbers = "";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

      new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
              new MethodChannel.MethodCallHandler() {
                  @Override
                  public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                      if (call.method.equals("getContacts")) {
                          phoneNumbers = getPhoneNumbers();

                          if (phoneNumbers != null) {
                              result.success(phoneNumbers);
                          } else {
                              result.error("UNAVAILABLE", "Contacts not found", null);
                          }
                      } else {
                          result.notImplemented();
                      }
                  }
              });
  }

  String getPhoneNumbers(){

      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
      while (phones.moveToNext())
      {
          String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

          if (phoneNumber != null)
            phoneNumbers = phoneNumbers + phoneNumber + ",";

      }
      return phoneNumbers;
  }
}

Flutter代码:

static const platform = const MethodChannel('samples.flutter.io/contact');

final String result = await platform.invokeMethod('getContacts');
List<String> phoneNumbers = result.split(",");

答案 2 :(得分:0)

空安全代码

Java 端(发送数据):

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine)
{
    super.configureFlutterEngine(flutterEngine);

    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "foo_channel")
            .setMethodCallHandler((methodCall, result) -> {
                if (methodCall.method.equals("methodInJava")) {
                    // Return your List here.
                    List<String> list = new ArrayList<String>();
                    list.add("A");
                    list.add("B");
                    list.add("C");
                    result.success(list);
                }
            });
}

Dart 端(接收数据):

void getList() async {
  var channel = MethodChannel('foo_channel');
  List<String>? list = await channel.invokeListMethod<String>('methodInJava');
  print(list); // [A, B, C]
}