gpt4 book ai didi

android - onSMSArrive cordova 短信插件执行了不止一次

转载 作者:行者123 更新时间:2023-11-29 01:20:51 25 4
gpt4 key购买 nike

我正在开发一款能够在用户收到短信时读取短信内容的应用。

所以我使用 ionic 和 cordova 短信插件来读取短信内容。但是当用户收到一条短信并触发插件提供的 onSMSArrive 事件时,它确实起作用并且可以读取短信内容。

问题是它执行(读取短信)不止一次,准确地说是树次。

我将这段代码作为服务放在 ionic 中。

app.factory('$smsarrive', [function() {
return {
periksa:function() {

if (SMS) SMS.enableIntercept(true, function() {
console.log("some debug hint here");
}, function(){
console.log("some debug hint here");
});

if(SMS) SMS.startWatch(function() {
//update('watching', 'watching started');
console.log("some debug hint here");
}, function(){
//updateStatus('failed to start watching');
console.log("some debug hint here");
});

document.addEventListener('onSMSArrive', function(e) {
var sms = e.data;
var isiSms = sms.body;

if (isiSms.match(/FC0019229/g)!=null) {
if (isiSms.match(/Berhasil/g)!=null) {
console.log("Isi pulsa Berhasil");
} else if (isiSms.match(/Gagal/g)) {
console.log("Isi pulsa Gagal");
} else {
console.log(isiSms);
}
} else {
console.log("some hint here");
}
console.log("ASLI : "+isiSms);
});
}

}
}])

并在 View 的 Controller 是时执行该服务

$scope.$on('$ionicView.enter', function() {
$smsarrive.periksa();
})

有什么建议吗?也很抱歉英语不好。

我用 this plugin

最佳答案

根据我在您的代码示例中的理解,每次输入 View 时您都在执行函数“periksa”(这就是触发“$ionicView.enter”事件的原因)。然后,每次执行“periksa”(每次输入 View )时,您都会为 SMS 消息创建一个 EventListener。

因此,如果您输入了三个 View ,您将在 SMSArrive 上触发三个监听器以接收 SMS 的到达。

因此,我认为您应该只在启动应用程序时(在 app.js 中,在 .run 中,当 $ionicPlatform.ready() 时)开始观看并添加一次 EventListener。

.run(function($ionicPlatform, $smsarrive) {
$ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);

}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}

if(( /(ipad|iphone|ipod|android)/i.test(navigator.userAgent) )) {

if (! SMS ) { alert( 'SMS plugin not ready' ); return; }

SMS.startWatch(function(){
console.log('Watching');
}, function(){
console.log('Not Watching');
});

document.addEventListener('onSMSArrive', function(e){
var data = e.data;
$smsarrive.periksa(data);
});

} else {
alert('need run on mobile device for full functionalities.');
}
});

然后,您的“periksa”函数应该只接收数据作为参数,用于处理和处置结果。

如果这有帮助,请告诉我,我也在研究这个框架,我的应用程序中有这个插件。

关于android - onSMSArrive cordova 短信插件执行了不止一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36890452/

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