1.增加获取手机有几个手机卡的功能

2.使用指定的手机卡发送短信
This commit is contained in:
kun 2025-03-15 01:16:38 +08:00
parent 93d7ac3988
commit bb5059e0a9
6 changed files with 104 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.telephony.SmsManager
import android.telephony.SubscriptionManager
import android.util.Log
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
@ -73,10 +74,67 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
sendSMS(result, recipients, message!!, sendDirect)
}
"canSendSMS" -> result.success(canSendSMS())
"getAvailableSIMs" -> result.success(getAvailableSIMs())
"sendSMSWithSubscriptionId" -> {
val message = call.argument<String?>("message") ?: ""
val recipients = call.argument<String?>("recipients") ?: ""
val subscriptionId = call.argument<Int?>("subscriptionId") ?: -1
sendSMSWithSubscriptionId(result, recipients, message, subscriptionId)
}
else -> result.notImplemented()
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private fun sendSMSWithSubscriptionId(result: Result, phones: String, message: String, subscriptionId: Int) {
// 创建短信发送状态的 PendingIntent
val sentIntent = PendingIntent.getBroadcast(activity, 0, Intent("SMS_SENT_ACTION"), PendingIntent.FLAG_IMMUTABLE)
// 根据 subscriptionId 获取对应的 SmsManager 实例
val smsManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && subscriptionId != -1) {
SmsManager.getSmsManagerForSubscriptionId(subscriptionId)
} else {
SmsManager.getDefault()
}
// 将电话号码字符串按分号分割成多个号码
val numbers = phones.split(";")
for (num in numbers) {
// 打印短信长度信息
Log.d("Flutter SMS", "msg.length() : " + message.toByteArray().size)
if (message.toByteArray().size > 80) {
// 短信长度超过 80 字节,分割短信
val partMessage = smsManager.divideMessage(message)
// 发送分割后的多条短信
smsManager.sendMultipartTextMessage(num, null, partMessage, null, null)
} else {
// 短信长度未超过 80 字节,直接发送单条短信
smsManager.sendTextMessage(num, null, message, sentIntent, null)
}
}
// 短信发送成功,返回结果
result.success("SMS Sent with subscription ID!")
}
// 修改 getAvailableSIMs 方法,只返回 SIM 卡 ID
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private fun getAvailableSIMs(): List<Int> {
val availableSIMs = mutableListOf<Int>()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
val subscriptionManager = activity?.getSystemService(Activity.TELEPHONY_SUBSCRIPTION_SERVICE) as? SubscriptionManager
if (subscriptionManager != null) {
val subscriptions = subscriptionManager.activeSubscriptionInfoList
if (subscriptions != null) {
for (subscription in subscriptions) {
availableSIMs.add(subscription.subscriptionId)
}
}
}
}
return availableSIMs
}
@TargetApi(Build.VERSION_CODES.ECLAIR)
private fun canSendSMS(): Boolean {
if (!activity!!.packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY))

View File

@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_sms","path":"/home/david/code/forks/flutter_sms/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"url_launcher_ios","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_ios-6.3.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"android":[{"name":"flutter_sms","path":"/home/david/code/forks/flutter_sms/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"url_launcher_android","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_android-6.3.14/","native_build":true,"dependencies":[],"dev_dependency":false}],"macos":[{"name":"url_launcher_macos","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_macos-3.2.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"linux":[{"name":"url_launcher_linux","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_linux-3.2.1/","native_build":true,"dependencies":[],"dev_dependency":false}],"windows":[{"name":"url_launcher_windows","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_windows-3.1.4/","native_build":true,"dependencies":[],"dev_dependency":false}],"web":[{"name":"flutter_sms","path":"/home/david/code/forks/flutter_sms/","dependencies":[],"dev_dependency":false},{"name":"url_launcher_web","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_web-2.4.0/","dependencies":[],"dev_dependency":false}]},"dependencyGraph":[{"name":"flutter_sms","dependencies":["url_launcher"]},{"name":"url_launcher","dependencies":["url_launcher_android","url_launcher_ios","url_launcher_linux","url_launcher_macos","url_launcher_web","url_launcher_windows"]},{"name":"url_launcher_android","dependencies":[]},{"name":"url_launcher_ios","dependencies":[]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2025-02-21 17:07:22.951360","version":"3.29.0","swift_package_manager_enabled":{"ios":false,"macos":false}}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_sms","path":"/home/kun/桌面/flutter_sms/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"url_launcher_ios","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_ios-6.3.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"android":[{"name":"flutter_sms","path":"/home/kun/桌面/flutter_sms/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"url_launcher_android","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_android-6.3.14/","native_build":true,"dependencies":[],"dev_dependency":false}],"macos":[{"name":"url_launcher_macos","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_macos-3.2.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"linux":[{"name":"url_launcher_linux","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_linux-3.2.1/","native_build":true,"dependencies":[],"dev_dependency":false}],"windows":[{"name":"url_launcher_windows","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_windows-3.1.4/","native_build":true,"dependencies":[],"dev_dependency":false}],"web":[{"name":"flutter_sms","path":"/home/kun/桌面/flutter_sms/","dependencies":[],"dev_dependency":false},{"name":"url_launcher_web","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_web-2.4.0/","dependencies":[],"dev_dependency":false}]},"dependencyGraph":[{"name":"flutter_sms","dependencies":["url_launcher"]},{"name":"url_launcher","dependencies":["url_launcher_android","url_launcher_ios","url_launcher_linux","url_launcher_macos","url_launcher_web","url_launcher_windows"]},{"name":"url_launcher_android","dependencies":[]},{"name":"url_launcher_ios","dependencies":[]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2025-03-15 01:06:57.079472","version":"3.29.1","swift_package_manager_enabled":{"ios":false,"macos":false}}

View File

@ -6,7 +6,8 @@
FlutterApplication and put your custom class here. -->
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:name="${applicationName}"
android:label="example"

View File

@ -29,11 +29,13 @@ class _MyAppState extends State<MyApp> {
}
Future<void> _sendSMS(List<String> recipients) async {
var c = await getAvailableSIMs();
print(c);
try {
String _result = await sendSMS(
String _result = await sendSMSWithSubscriptionId(
message: _controllerMessage.text,
recipients: recipients,
sendDirect: sendDirect,
subscriptionId: 1,
);
setState(() => _message = _result);
} catch (error) {

View File

@ -30,3 +30,17 @@ Future<bool> launchSmsMulti({
/// Check if you can send SMS on this platform
Future<bool> canSendSMS() => FlutterSmsPlatform.instance.canSendSMS();
Future<List<int>> getAvailableSIMs() => FlutterSmsPlatform.instance.getAvailableSIMs();
// sendSMSWithSubscriptionId
Future<String> sendSMSWithSubscriptionId({
required String message,
required List<String> recipients,
required int subscriptionId,
})=>
FlutterSmsPlatform.instance.sendSMSWithSubscriptionId(
message: message,
recipients: recipients,
subscriptionId: subscriptionId,
);

View File

@ -56,6 +56,31 @@ class FlutterSmsPlatform extends PlatformInterface {
}
}
/// SIM ID
Future<List<int>> getAvailableSIMs() {
return _channel
.invokeMethod<List<dynamic>>('getAvailableSIMs')
.then((value) {
return value?.map((simId) => simId as int).toList() ?? [];
});
}
/// SIM
Future<String> sendSMSWithSubscriptionId({
required String message,
required List<String> recipients,
required int subscriptionId,
}) {
final mapData = <dynamic, dynamic>{};
mapData['message'] = message;
final _phones = recipients.join(';');
mapData['recipients'] = _phones;
mapData['subscriptionId'] = subscriptionId;
return _channel
.invokeMethod<String>('sendSMSWithSubscriptionId', mapData)
.then((value) => value ?? 'Error sending sms');
}
Future<bool> canSendSMS() {
return _channel
.invokeMethod<bool>('canSendSMS')