diff --git a/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt b/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt index 25af08c..93c8308 100644 --- a/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt +++ b/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt @@ -3,7 +3,10 @@ package com.example.flutter_sms import android.annotation.TargetApi import android.app.Activity import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.Context import android.content.Intent +import android.content.IntentFilter import android.content.pm.PackageManager import android.net.Uri import android.os.Build @@ -21,11 +24,14 @@ import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.PluginRegistry.Registrar -class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { +class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware , + BroadcastReceiver() { private lateinit var mChannel: MethodChannel private var activity: Activity? = null private val REQUEST_CODE_SEND_SMS = 205 + var result: Result? = null + override fun onAttachedToActivity(binding: ActivityPluginBinding) { activity = binding.activity } @@ -68,6 +74,8 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { inst.activity = registrar.activity() inst.setupCallbackChannels(registrar.messenger()) } + + const val SENT_SMS_ACTION_NAME = "SMS_SENT_ACTION" } override fun onMethodCall(call: MethodCall, result: Result) { @@ -110,6 +118,11 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { } private fun sendSMSDirect(result: Result, phones: String, message: String) { + this.result = result + val intentFilter = IntentFilter() + intentFilter.addAction(SENT_SMS_ACTION_NAME) + activity?.registerReceiver(this, intentFilter) + // SmsManager is android.telephony val sentIntent = PendingIntent.getBroadcast(activity, 0, Intent("SMS_SENT_ACTION"), PendingIntent.FLAG_IMMUTABLE) val mSmsManager = SmsManager.getDefault() @@ -125,7 +138,6 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { } } - result.success("SMS Sent!") } private fun sendSMSDialog(result: Result, phones: String, message: String) { @@ -136,4 +148,40 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { activity?.startActivityForResult(intent, REQUEST_CODE_SEND_SMS) result.success("SMS Sent!") } + + override fun onReceive(context: Context, intent: Intent) { + if (intent.action.equals(SENT_SMS_ACTION_NAME)) { + when (resultCode) { + Activity.RESULT_OK -> { + result?.success("SMS Sent!"); + } + + 111 -> { + result?.error("111", "RESULT_ERROR_NO_CREDIT", "RESULT_ERROR_NO_CREDIT") + } + + SmsManager.RESULT_ERROR_NO_SERVICE -> { + result?.error("${SmsManager.RESULT_ERROR_NO_SERVICE}", "RESULT_ERROR_NO_SERVICE", "No service for sending SMS") + } + + SmsManager.RESULT_ERROR_NULL_PDU -> { + result?.error("${SmsManager.RESULT_ERROR_NULL_PDU}", "RESULT_ERROR_NULL_PDU", "Null PDU") + + } + + SmsManager.RESULT_ERROR_RADIO_OFF -> { + result?.error("${SmsManager.RESULT_ERROR_RADIO_OFF}", "RESULT_ERROR_RADIO_OFF", "May airplane mode is turned off") + } + + else -> { + result?.error("${SmsManager.RESULT_ERROR_GENERIC_FAILURE}", "RESULT_ERROR_GENERIC_FAILURE", "RESULT_ERROR_GENERIC_FAILURE") + } + } + } + + activity?.unregisterReceiver( + this + ) + + } } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 6fb01f8..6fc2986 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -6,8 +6,16 @@ additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> - - + + + + + + + + + +