android - 如何使用 NativeScript 收听传入的短信?

标签 android angular android-intent nativescript

我想收听 RECEIVE_SMS Intent ,以在 Nativescript(使用 Angular2)中读取通过短信发送的一次性密码。我正在按照 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');
  });
}

最佳答案

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

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

关于android - 如何使用 NativeScript 收听传入的短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44218256/

相关文章:

java - 无法打开sqlite数据库,指定的目录或数据库文件不存在

Lollipop 上的Android drawText缺少字符

angular - ngModel 未正确绑定(bind)

android - Activity 恢复时再次发送 Intent

android - 如何使通知 Intent 恢复而不是新的 Intent ?

android - 如何调用 Android TV 软键盘进行编辑文本?

android - 识别USB口的adb命令

angular - 在 Angular 区域之外运行 ngrx/effect 以防止 Protractor 超时

angular - Angular7 中的 ElasticSearch Post 调用抛出 400 错误

java - 将值从 Activity 传递到适配器