我正在尝试在Android设备上收听任何收到的短信息,然后每当收到短信息时在我的flutter应用程序中显示祝酒词。我正在通过EventChannel连接到Flutter,并使用广播接收器检测短信。每当广播接收器检测到短信息时,如何发送events.success(message)
我尝试将广播接收器直接添加到EventChannel中,但没有成功。颤振短信包似乎也不起作用。
我的主要活动是这样的:

public class MainActivity extends FlutterActivity{
    public static final String STREAM = "com.myapp.thisapp/stream";
    public static final String TAG = "THIS IS A MESSAGE: ";


    @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

        new EventChannel(getFlutterView(), STREAM).setStreamHandler(
                new EventChannel.StreamHandler() {

                    @Override
                    public void onListen(Object args, final
EventChannel.EventSink events) {
                        //Send events.success() when SMS received
                        Log.w(TAG, "adding listener");
                    }

                    @Override
                    public void onCancel(Object args) {
                        Log.w(TAG, "cancelling listener");
                    }
                }
        );
  }

}

这是我的广播接收器的代码:
public class IncomingSmsBroadcastReceiver extends BroadcastReceiver {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(final Context context, final Intent intent) {

        if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
            final SmsMessage smsMessage = extractSmsMessage(intent);
            processMessage(context, smsMessage);
        }

    }

    private SmsMessage extractSmsMessage(final Intent intent) {

        final Bundle pudsBundle = intent.getExtras();
        final Object[] pdus = (Object[]) pudsBundle.get("pdus");
        final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);

        return smsMessage;

    }

    private void processMessage(final Context context, final SmsMessage smsMessage) {

        //TODO: Send message to event channel
    }

}

每当广播接收器遇到短消息时,我希望将消息的内容发送到EventChannel,EventChannel将消息文本发送到颤振前端。我该怎么做?

最佳答案

这是您应该在MainActivity中使用的,存储结果

        public class MainActivity extends FlutterActivity{
            public static final String STREAM = "com.myapp.thisapp/stream";
            public static final String TAG = "THIS IS A MESSAGE: ";
            public Result resultLater;


            @Override
          protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            GeneratedPluginRegistrant.registerWith(this);

            new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                    new MethodCallHandler() {
                        @Override
                        public void onMethodCall(MethodCall call, Result result) {
                            //store the reference for later access
                            resultLater = result;
                        }
                    });

           }

         public class IncomingSmsBroadcastReceiver extends BroadcastReceiver {

         private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

          @Override
          public void onReceive(final Context context, final Intent intent) {

            if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
                final SmsMessage smsMessage = extractSmsMessage(intent);
                processMessage(context, smsMessage);
            }

        }

          private SmsMessage extractSmsMessage(final Intent intent) {

            final Bundle pudsBundle = intent.getExtras();
            final Object[] pdus = (Object[]) pudsBundle.get("pdus");
            final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);

            return smsMessage;

         }

       }

       private void processMessage(final Context context, final SmsMessage smsMessage) {

            //here send back result, like this
            if(smsMessage.getMessageBody()!=null){
                 result.success(smsMessage.getMessageBody());
            }else{
                 result.error("Error", "Sms not found", null);
            }

         }

  }

现在,在您的主要活动中创建SMS广播子类,并在下面声明。当你收到短信的时候就可以访问结果。从你颤振的一侧,像往常一样进行频道呼叫。
按照参考文献中的docs sample进行操作。

关于java - 通过EventChannel将Android SMS BroadcastReceiver数据发送到Flutter,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54088421/

10-13 02:26