颤振方法通道

时间:2020-09-22 08:50:19

标签: android-studio kotlin smsmanager

我正在Flutter中实现一个方法通道,以使用默认的消息传递应用程序发送textMessage,但是它不起作用。其余所有程序流都可以正常工作,只是未发送消息。首先它试图发送消息,但是在设备消息传递应用程序中获得了失败状态,现在它什么也不做。相反,我的程序流从通道返回后,我在Android Studio中收到了此消息

V / PhoneWindow(5722):DecorView setVisiblity:可见性= 0,父级= android.view.ViewRootImpl@76c918b,此= DecorView @ a148268 [MainActivity]

我刚开始学习颤动并开始学习科特林 我正在Xiomi Redmi 6A,API 28上测试此代码

import androidx.annotation.NonNull
import android.Manifest
import android.view.View
import android.telephony.SmsManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {

      private val channel  = "com.myReflectionThoughts/sendConfirmation";

     override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine){
        super.configureFlutterEngine(flutterEngine)

    MethodChannel(flutterEngine.dartExecutor.binaryMessenger,channel).setMethodCallHandler{
        call,result->
        val arguments = call.arguments<Map<String,Any>>()
        if(call.method == "sendConfirmation"){
        var confirmationMessage = "Your Order has been Accepted !!\n"+"TOD:- "+arguments["Tod"]+" ,Amount:- \u20B9"+arguments["amount"]+" ,Address:- "+arguments["address"]
        requestPermissions(arrayOf(Manifest.permission.SEND_SMS),10)

            val smsManager = SmsManager.getDefault()
            smsManager.sendTextMessage(arguments["contact"].toString(),null,confirmationMessage,null,null)
            result.success("Confirmation Sent!!")
        }
        else{
            result.error("Pta nahi","Kuch galat ho gya",null)
        }
    }
    }
}

这是我从中调用MEthodChannel的dart文件

class SwipeOrderTile extends StatefulWidget {
  @override
  _SwipeOrderTileState createState() => _SwipeOrderTileState();
}

class _SwipeOrderTileState extends State<SwipeOrderTile> {
  // bool to check if we need to see the order delivery
  bool _showOrderDetails = false;
  Order ord;
  static const platformchannel =
  MethodChannel("com.myReflectionThoughts/sendConfirmation");

  Widget createCustomContainer(dynamic child) {
    return Container(
        margin: EdgeInsets.all(2),
        padding: EdgeInsets.all(2),
        height: 25,
        width: double.infinity,
        child: child);
  }



  @override
  Widget build(BuildContext context) {
    final order = Provider.of<Order>(context);
    ord = order;

    return Dismissible(
      key: ValueKey(order.orderId),
      dismissThresholds: {DismissDirection.endToStart: 0.40},
      confirmDismiss: (direction) {
        if (direction == DismissDirection.endToStart)
          // Code To Remove this Order
          return showDialog(
              context: context,
              builder: (ctx) {
                return AlertDialog(
                  title: Text(
                    '!! Confirm Action !!',
                    style: TextStyle(fontWeight: FontWeight.bold),
                    textAlign: TextAlign.center,
                  ),
                  contentPadding: const EdgeInsets.all(4),
                  content: Text(
                    'Accept this Order ?',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                    ),
                    textAlign: TextAlign.center,
                  ),
                  actions: [
                    FlatButton(
                        child: Text('Yes',
                            style: TextStyle(fontWeight: FontWeight.bold)),
                        onPressed: () async {
                          try {
                            Navigator.of(context).pop(false);

                            final dataMap = <String, dynamic>{
                              'Tod': order.timeOfDelivery,
                              'amount': order.amount,
                              'address': order.address,
                              'contactNumber': order.contact
                            };
                         final response = await platformchannel.invokeMethod("sendConfirmation", dataMap);
                             print(response + " DartFile");
                           await Provider.of<RestaurantManager>(context,
                            listen: false)
                            .setOrderStatus(order.serverId, true);
                          } catch (error) {
                            print(error.toString());
                            print("Error Occured while Sending Message");
                          }
                        }),
                    FlatButton(
                      child: Text('NO',
                          style: TextStyle(fontWeight: FontWeight.bold)),
                      onPressed: () {
                        Navigator.of(context).pop(false);
                      },
                    )
                  ],
                );
              });
        else {
          return showDialog(
              context: context,
              builder: (ctx) {
                return AlertDialog(
                  title: Text(
                    '!! Confirm Action !!',
                    textAlign: TextAlign.center,
                  ),
                  contentPadding: const EdgeInsets.all(4),
                  content: Text(
                    'Decline this Order ?',
                    textAlign: TextAlign.center,
                  ),
                  actions: [
                    FlatButton(
                        child: Text('Yes'),
                        onPressed: () {
                          Navigator.of(context).pop(true);
                          Provider.of<RestaurantManager>(context, listen: false)
                              .setOrderStatus(order.serverId, false);
                        }),
                    FlatButton(
                      child: Text('NO'),
                      onPressed: () {
                        Navigator.of(context).pop(false);
                      },
                    )
                  ],
                );
              });
        }
      },

 

0 个答案:

没有答案