其实官网上很清楚,只要跟着操作几遍,其意自现:
https://flutter.cn/docs/platform-integration/platform-channels?tab=type-mappings-kotlin-tab#separate
以安卓示例总结步骤
- 在flutter页面中创建获取方法管道(管道名命名规则:域名倒序/管道名称)
- 创建方法结果的初始状态
- 封装异步方法用于获取不同类型的信息
- 创建具体执行的异步方法
- 这一步开始在安卓操作,实现原生方法或系统能力,先同样定义一个与步骤1方法名相同管道
- 配置Flutter引擎->创建一个方法通道并设置方法调用处理程序
- 功能实现(处理程序)
flutter页面示例代码
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Classify extends StatefulWidget {
const Classify({super.key});
@override
State<Classify> createState() => _ClassifyState();
}
class _ClassifyState extends State<Classify> {
// 创建获取通讯录方法管道
static const platform = MethodChannel('samples.flutter.dev/contacts');
// 创建通讯录初始状态
String _contacts = '未获取通讯录.';
// 创建网络状态初始状态
String _networkStatus = '未获取网络状态.';
// 创建短信初始状态
String _sms = '未获取短信.';
// 异步方法用于获取不同类型的信息
Future<String> _fetchInformation(String method, String errorMessage) async {
try {
final result = await platform.invokeMethod(method);
return '$errorMessage: $result .';
} on PlatformException catch (e) {
return "Failed to get $errorMessage: '${e.message}'.";
}
}
// 创建通讯录异步方法
Future<void> _getContacts() async {
String contacts = await _fetchInformation('getContacts', '通讯录');
setState(() {
_contacts = contacts;
});
}
// 创建网络状态异步方法
Future<void> _getNetworkStatus() async {
String networkStatus = await _fetchInformation('getNetworkStatus', '网络状态');
setState(() {
_networkStatus = networkStatus;
});
}
// 创建短信异步方法
Future<void> _getSms() async {
String sms = await _fetchInformation('getSms', '短信');
setState(() {
_sms = sms;
});
}
@override
Widget build(BuildContext context) {
// listview展现通讯录,姓名+电话
return Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: _getContacts,
child: const Text('获取通讯录'),
),
ElevatedButton(
onPressed: _getNetworkStatus,
child: const Text('获取网络状态'),
),
ElevatedButton(
onPressed: _getSms,
child: const Text('获取短信'),
),
Text(_contacts),
Text(_networkStatus),
Text(_sms),
],
),
),
);
}
}
安卓实现示例代码
package com.example.flutter02
import android.annotation.SuppressLint
import android.content.ContentResolver
import android.database.Cursor
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.provider.ContactsContract.CommonDataKinds.Phone
import android.provider.ContactsContract.Contacts
import androidx.annotation.NonNull
import androidx.annotation.RequiresApi
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.lang.Boolean
class Contacts: FlutterActivity() {
// 定义一个方法通道名称
private val CHANNEL = "samples.flutter.dev/contacts"
//配置Flutter引擎
@RequiresApi(Build.VERSION_CODES.M)
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
// 创建一个方法通道并设置方法调用处理程序
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
try {
when (call.method) {
"getContacts" -> {
// 处理"getContacts"方法调用
val contacts = getContacts()
if (contacts != null) {
result.success(contacts)
} else {
result.error("ERROR", "Unable to get contacts.", null)
}
}
"getNetworkStatus" -> {
// 处理"getNetworkStatus"方法调用
val networkStatus = getNetworkStatus()
if (networkStatus != null) {
result.success(networkStatus)
} else {
result.error("ERROR", "Unable to get network status.", null)
}
}
"getSms" -> {
// 处理"getSms"方法调用
val sms = getSms()
if (sms != null) {
result.success(sms)
} else {
result.error("ERROR", "Unable to get sms.", null)
}
}
else -> {
// 如果调用的方法不是已实现的方法,返回未实现错误
result.notImplemented()
}
}
} catch (e: Exception) {
// 处理异常情况,返回错误信息给Flutter
result.error("ERROR", e.message, null)
}
}
}
// 获取手机通讯录联系人的示例方法
@SuppressLint("Range")
private fun getContacts(): List<Map<String, String>>? {
val contactsList = mutableListOf<Map<String, String>>()
// 获取ContentResolver以访问手机数据
val contentResolver: ContentResolver = applicationContext.contentResolver
// 查询手机通讯录的联系人
val cursor: Cursor? = contentResolver.query(
Contacts.CONTENT_URI, null, null, null, null
)
cursor?.use {
while (it.moveToNext()) {
val contactId = it.getString(it.getColumnIndex(Contacts._ID))
val name = it.getString(it.getColumnIndex(Contacts.DISPLAY_NAME))
// 查询联系人的电话号码
val phoneCursor: Cursor? = contentResolver.query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = ?", arrayOf(contactId), null
)
phoneCursor?.use { phone ->
while (phone.moveToNext()) {
val phoneNumber = phone.getString(phone.getColumnIndex(Phone.NUMBER))
// 将联系人信息存储在Map中
val contactInfo = mutableMapOf<String, String>()
contactInfo["name"] = name
contactInfo["phone"] = phoneNumber
// 将联系人信息添加到列表中
contactsList.add(contactInfo)
}
}
phoneCursor?.close()
}
}
cursor?.close()
// 返回包含联系人信息的列表
return contactsList
}
@RequiresApi(Build.VERSION_CODES.M)
private fun getNetworkStatus(): Map<String, String>? {
// 获取ConnectivityManager以获取网络状态
val connectivityManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
// 获取当前活动的网络信息
val activeNetwork = connectivityManager.activeNetwork
// 如果网络信息不为空,返回网络状态
if (activeNetwork != null) {
val networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork)
val networkStatus: MutableMap<String, String> = HashMap()
networkStatus["type"] =
if (networkCapabilities!!.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) "WIFI" else "CELLULAR"
networkStatus["isConnected"] =
Boolean.toString(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED))
return networkStatus
}
// 如果网络信息为空,返回null
return null
}
// 获取所有手机短信内容
@SuppressLint("Range")
private fun getSms(): List<Map<String, String>>? {
val smsList = mutableListOf<Map<String, String>>()
// 获取ContentResolver以访问手机数据
val contentResolver: ContentResolver = applicationContext.contentResolver
// 查询手机短信
val cursor: Cursor? = contentResolver.query(
android.provider.Telephony.Sms.CONTENT_URI, null, null, null, null
)
cursor?.use {
while (it.moveToNext()) {
val smsId = it.getString(it.getColumnIndex(android.provider.Telephony.Sms._ID))
val smsAddress = it.getString(it.getColumnIndex(android.provider.Telephony.Sms.ADDRESS))
val smsBody = it.getString(it.getColumnIndex(android.provider.Telephony.Sms.BODY))
val smsDate = it.getString(it.getColumnIndex(android.provider.Telephony.Sms.DATE))
// 将短信信息存储在Map中
val smsInfo = mutableMapOf<String, String>()
smsInfo["smsId"] = smsId
smsInfo["smsAddress"] = smsAddress
smsInfo["smsBody"] = smsBody
smsInfo["smsDate"] = smsDate
// 将短信信息添加到列表中
smsList.add(smsInfo)
}
}
cursor?.close()
// 返回包含短信信息的列表
return smsList
}
}