gpt4 book ai didi

java - 通过EventChannel向Flutter发送Android短信BroadcastReceiver数据

转载 作者:IT王子 更新时间:2023-10-29 06:54:09 25 4
gpt4 key购买 nike

我正在尝试在 Android 设备上收听任何传入的 SMS 消息,然后在收到 SMS 时在我的 Flutter 应用程序中显示 toast。我通过 EventChannel 连接到 Flutter 并使用 BroadcastReceiver 检测 SMS。每当我的广播接收器检测到 SMS 时,如何发送 events.success(message)

我尝试将 BroadcastReceiver 直接添加到 EventChannel 中,但这没有用。 flutter SMS 包似乎也不起作用。

这是我的 MainActivity 的样子:

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");
}
}
);
}

}

这是我的 BroadcastReceiver 的代码:

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
}

}

每当 BroadCastReceiver 遇到 SMS 时,我希望将消息的内容发送到 EventChannel,EventChannel 会将消息文本发送到 Flutter 前端。我该怎么做?

最佳答案

根据您的第一条评论 Laksh22是的,这是可能的。

像这样为您的“Activity ”创建一个构造函数:

     _yourclassnameState() {
platform.setMethodCallHandler(JavaMethodHandler);
}

然后实现响应处理程序

       Future<dynamic> JavaMethodHandler(MethodCall methodcall) async
{
switch(methodcall.method)
{
case 'SMSRecived':
print('DataRecived is' + methodcall.arguments);
break;
default:
break;
}
}

然后在您的“BroadcastReceiver”中使用此代码调用 flutter 方法:

    MethodChannel channel =new MethodChannel(view,CHANNEL);
channel.invokeMethod("SMSRecived",args, new MethodChannel.Result() {
@Override
public void success(Object o) {
System.out.println(o);
}

@Override
public void error(String s, String s1, Object o) {

}

@Override
public void notImplemented() {

}
});

Don't forget about CHANNEL. It should be the same both side.

Don't forget about manifest file.

For receiving SMS while your app is closed use 'ForegroundService'.Unfortunately Flutter dosen't support ForegroundService yet. you should implement it in Java.

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com