Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用NativeScript收听传入的sms?_Android_Angular_Android Intent_Nativescript - Fatal编程技术网

Android 如何使用NativeScript收听传入的sms?

Android 如何使用NativeScript收听传入的sms?,android,angular,android-intent,nativescript,Android,Angular,Android Intent,Nativescript,我想收听接收短信,以阅读通过文本消息发送的一次性密码,使用Nativescript(使用Angular2)。我正在遵循中概述的步骤。但是,当我运行代码时,会出现以下错误: 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: ',' expe

我想收听接收短信,以阅读通过文本消息发送的一次性密码,使用Nativescript(使用Angular2)。我正在遵循中概述的步骤。但是,当我运行代码时,会出现以下错误:

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');
  });
}
从“@angular/core”导入{Component};
@组成部分({
选择器:“我的应用程序”,
模板:`
`
})
导出类AppComponent{
//你的打字脚本逻辑在这里
app.android.registerBroadcastReceiver(android.provider.Telephony.SMS_已接收,函数onReceiveCallback(上下文:android.content.context,意图:android.content.intent){
控制台日志(“rcvd”);
});
}

您的语法不正确。您应该将监听功能放在
ngOnInit
hook中:

import {OnInit} from '@angular/core'

export class AppComponent implements OnInit {

    ngOnInit() {

    // your code here
    }
}

看起来您没有导入所需的模块

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');
 });}
}
从“@angular/core”导入{Component,OnInit};
从“tns核心模块/应用程序”导入*作为应用程序;
从“tns核心模块/平台”导入*作为平台;
//声明此项以使用android变量
声明:任何;
@组成部分({
选择器:“我的应用程序”,
模板:`
`
})
导出类AppComponent{
恩戈尼尼特(){
app.android.registerBroadcastReceiver(android.provider.Telephony.SMS_已接收,函数onReceiveCallback(上下文:android.content.context,意图:android.content.intent){
控制台日志(“rcvd”);
});}
}

typescript编译器还需要android typesCan的node_模块中的
tns平台声明
文件夹。请建议逐步添加SMS接收器,因为我已经添加了“tns核心模块”和“tns平台声明”,但仍然无法解析“tns核心模块/应用程序”