我使用nativescript建立一個應用程序,將程序在緊急情況下發送預建的文本預設的多個政黨。如何使用廣播reciever擺脫smsmanager(nativescript)發送意圖
我有一個電話號碼的數組,並希望遍歷每一個,使用SMSmanager發送文本和sentIntent參數seen in android docs驗證文本發送之前,移動到下一個數組項。
我已經創建了的PendingIntent變量傳遞到 「sms.sendTextMessage」 如下:
var sms = android.telephony.SmsManager.getDefault();
var utils = require("utils/utils");
//Gets application's current state
var context = utils.ad.getApplicationContext();
//Create a replica of Android's intent object
var intent = new android.content.Intent(context, com.tns.NativeScriptActivity.class);
//Create a replica of Android's pendingIntent object using context and intent
var pendingIntent = android.app.PendingIntent.getActivity(context, 1, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
然後我發送的文字,傳遞掛起意圖VAR:
sms.sendTextMessage("5555555555", null, "hello", pendingIntent, null);
然後我嘗試使用我在nativescript docs中找到的信息製作基本的廣播接收器,當接收到預期數據時,它應該將某些內容記錄到控制檯。
app.android.registerBroadcastReceiver(pendingIntent, function() {
console.log("@@@@@ text sent @@@@@");
});
問題是:沒有任何反應。我希望得到「@@@@@ text @@@@@」登錄到控制檯我已經搜索了很多,我想我可能需要在清單中添加一些關於這個廣播接收者的內容,或者也許我的執行力度是錯的,但畢竟是我的Android應用程序第一次破裂和我在一個小的損失的。任何幫助,將不勝感激。
我不會做NativeScript,但是如果你在運行時註冊Receiver,你需要發送一個隱含的'Intent';即一個'Intent'實例化了一個唯一的,任意的'String'動作參數,而不是'Context'和'Class'。然後,您需要爲Receiver註冊'String'動作,看起來像這樣做,該動作應該是您的registerBroadcastReceiver()調用中的第一個參數,而不是「pendingIntent」。而你的'PendingIntent'需要用'getBroadcast()'而不是'getActivity()'獲得。 –
嘿,這幫助我找到解決方案:)謝謝!我提到的代碼 – HelloWorld