【问题标题】:How to listen to an incoming sms using NativeScript?如何使用 NativeScript 收听传入的短信?
【发布时间】:2017-05-27 14:50:46
【问题描述】:

我想在 Nativescript(使用 Angular2)中收听 RECEIVE_SMS 意图以读取通过文本消息传递的一次性密码。我正在遵循http://docs.nativescript.org/cookbook/application 中列出的步骤。但是,当我运行代码时,出现以下错误:

app/app.component.ts(12,6): error TS1005: ';' expected.
app/app.component.ts(12,14): error TS1005: ';' expected.
app/app.component.ts(12,48): error TS1005: ',' expected.
app/app.component.ts(12,57): error TS1005: ',' expected.
app/app.component.ts(12,67): error TS1005: ',' expected.
app/app.component.ts(12,82): error TS1138: Parameter declaration expected.
app/app.component.ts(12,118): error TS2503: Cannot find namespace 'android'.
app/app.component.ts(12,151): error TS2503: Cannot find namespace 'android'.
app/app.component.ts(13,36): error TS2304: Cannot find name 'android'.
app/app.component.ts(14,36): error TS2304: Cannot find name 'android'.
app/app.component.ts(17,4): error TS1128: Declaration or statement expected.
app/app.component.ts(18,1): error TS1128: Declaration or statement expected.

这是我的 app.component.ts:

import { Component } from "@angular/core";

@Component({
  selector: "my-app",
  template: `
    <ActionBar title="My App"></ActionBar>
    <!-- Your UI components go here -->
  `
})
export class AppComponent {
  // Your TypeScript logic goes here
  app.android.registerBroadcastReceiver(android.provider.Telephony.SMS_RECEIVED, function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
    console.log('rcvd');
  });
}

【问题讨论】:

    标签: android angular android-intent nativescript


    【解决方案1】:

    您似乎没有导入所需的模块

    import { Component, OnInit } from "@angular/core";
    import * as app from "tns-core-modules/application";
    import * as platform from "tns-core-modules/platform";
    
    //declare this to use android variable
    declare var android:any;
    
    @Component({
      selector: "my-app",
      template: `
        <ActionBar title="My App"></ActionBar>
        <!-- Your UI components go here -->
       `
     })
    export class AppComponent{
    
     ngOnInit(){
    app.android.registerBroadcastReceiver(android.provider.Telephony.SMS_RECEIVED, function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
    console.log('rcvd');
     });}
    }
    

    【讨论】:

    • typescript 编译器还需要node_modules 中的tns-platform-declarations 文件夹与android 类型
    • 您能否建议逐步添加 SMS 接收器的过程,因为我添加了“tns-core-modules”和“tns-platform-declarations”,但它仍然给我无法解决'tns-core-modules/应用程序'
    【解决方案2】:

    您的语法不正确。你应该把你的监听函数放在ngOnInit钩子里:

    import {OnInit} from '@angular/core'
    
    export class AppComponent implements OnInit {
    
        ngOnInit() {
    
        // your code here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多