我在另一个stackoverflow线程中找到了这段代码how to send an SMS without users interaction in flutter?。 但它似乎不再起作用了。当我开始颤动时,应用程序正在组装并启动,但随后它会自动崩溃。
我认为问题在于Android代码。不幸的是,IntelliJ没有显示任何错误。
package com.yourcompany.example;
import android.telephony.SmsManager;
import android.util.Log;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "sendSms";
private MethodChannel.Result callResult;
@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("send")){
String num = call.argument("phone");
String msg = call.argument("msg");
sendSMS(num,msg,result);
}else{
result.notImplemented();
}
}
});
}
private void sendSMS(String phoneNo, String msg,MethodChannel.Result result) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
result.success("SMS Sent");
} catch (Exception ex) {
ex.printStackTrace();
result.error("Err","Sms Not Sent","");
}
}
}
答案 0 :(得分:1)
您可以按如下方式修改Android代码,这应该可以。
Android的代码:
package com.yourcompany.smsdemo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "sendSms";
// private MethodChannel.Result callResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.SEND_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
requestPermissions(
new String[]{Manifest.permission.SEND_SMS},
0);
// MY_PERMISSIONS_REQUEST_SEND_SMS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("send")) {
String num = call.argument("phone");
String msg = call.argument("msg");
sendSMS(num, msg, result);
} else {
result.notImplemented();
}
}
});
}
private void sendSMS(String phoneNo, String msg, MethodChannel.Result result) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
result.success("SMS Sent");
} catch (Exception ex) {
ex.printStackTrace();
result.error("Err", "Sms Not Sent", "");
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
}
并在AndroidManifest.xml中包含以下行
<uses-permission android:name="android.permission.SEND_SMS" />
我实际修改了@ HemanthRaj的代码并添加了here的运行时权限。 如果有效,请告诉我。非常感谢
答案 1 :(得分:0)
答案 2 :(得分:0)
这是@Mahi解决方案的Kotlin
代码:
import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.telephony.SmsManager
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterActivity() {
// private MethodChannel.Result callResult;
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.SEND_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
requestPermissions(arrayOf(Manifest.permission.SEND_SMS),
0)
// MY_PERMISSIONS_REQUEST_SEND_SMS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
MethodChannel(getBinaryMessenger(), CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "send") {
val num = call.argument<String>("phone")
val msg = call.argument<String>("msg")
sendSMS(num, msg, result)
} else {
result.notImplemented()
}
}
}
private fun getBinaryMessenger(): BinaryMessenger? {
return flutterEngine!!.dartExecutor.binaryMessenger
}
private fun sendSMS(phoneNo: String?, msg: String?, result: MethodChannel.Result) {
try {
val smsManager = SmsManager.getDefault()
smsManager.sendTextMessage(phoneNo, null, msg, null, null)
result.success("SMS Sent")
} catch (ex: Exception) {
ex.printStackTrace()
result.error("Err", "Sms Not Sent", "")
}
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
0 -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.isNotEmpty()
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return
}
}
}
companion object {
private const val CHANNEL = "sendSms"
}
}