唉, 安卓代碼轉(zhuǎn)成autojs使用的代碼,有些代碼的轉(zhuǎn)換真的是蛋疼的很,一個(gè)小小的問(wèn)題.折騰了我三天,到處翻資料, 到頭來(lái)答案卻在眼皮子上掛著...不說(shuō)了,丟人...
想要使用autojs在后臺(tái)靜默發(fā)送短信很簡(jiǎn)單,簡(jiǎn)簡(jiǎn)單單一句代碼就搞定,但是還得需要判斷是否發(fā)送成功了, 對(duì)方是否接收成功了等等問(wèn)題.
//Autojs Code:
importClass(android.telephony.gsm.SmsManager);
SmsManager.getDefault().sendTextMessage(phoneNumber, null, msg, sentPI, deliverPI);
說(shuō)一下各個(gè)參數(shù)的意義: 以實(shí)際傳的參數(shù)來(lái)說(shuō)明,只說(shuō)作用,不說(shuō)官方解釋
phoneNumber : 就是接收短信的手機(jī)號(hào)碼, string類型,
null : 短信服務(wù)中心號(hào)碼, 默認(rèn)null,
msg : 短信內(nèi)容, 因?yàn)槭莝endTextMessage,所以這個(gè)參數(shù)是string類型,
sentPI : 發(fā)送時(shí)要觸發(fā)的intent,
deliverPI : 對(duì)方接收到短信所觸發(fā)的intent
要判斷是否發(fā)送成功和接收成功,就需要注冊(cè)兩個(gè)廣播的監(jiān)聽(tīng).來(lái)得到結(jié)果.下面分別記錄一下免費(fèi)版Autojs和AutojsPro8.0以上的版本如何注冊(cè)一個(gè)監(jiān)聽(tīng).
免費(fèi)版注冊(cè)監(jiān)聽(tīng)的方式
/**
* 免費(fèi)版autojs注冊(cè)監(jiān)聽(tīng)
*/
var sendMessageFilter=new IntentFilter();
sendMessageFilter.addAction(SENT_SMS_ACTION);
var sendMessage = new JavaAdapter(android.content.BroadcastReceiver, {
onReceive : function(context, intent) {
let phone = intent.getStringExtra("KEY_PHONENUM");
let message = intent.getStringExtra("KEY_MESSAGE");
switch (sendMessage.getResultCode()) {
case Activity.RESULT_OK:
console.log("給手機(jī)號(hào)碼: " + phone + " 發(fā)送短信成功");
console.log("發(fā)送的內(nèi)容: " + message);
break;
default:
console.log("給手機(jī)號(hào)碼: " + phone + " 發(fā)送短信成功");
break;
}
}
});
context.registerReceiver(sendMessage,sendMessageFilter);
var receiverFilter=new IntentFilter();
receiverFilter.addAction(DELIVERED_SMS_ACTION);
var receiver = new JavaAdapter(android.content.BroadcastReceiver, {
onReceive : function(context, intent) {
let phone = intent.getStringExtra("KEY_PHONENUM");
console.log("手機(jī)號(hào): " + phone + " 接收短信成功");
}
});
context.registerReceiver(receiver, receiverFilter);
以下是Pro8.0以后的版本注冊(cè)監(jiān)聽(tīng)的方式:
/**
* autojsPro 8.0以上的版本使用的監(jiān)聽(tīng)
*/
//發(fā)送短信的監(jiān)聽(tīng)
let sendMessage = new BroadcastReceiver(function(ctx, intent) {
let phone = intent.getStringExtra("KEY_PHONENUM");
let message = intent.getStringExtra("KEY_MESSAGE");
switch (sendMessage.getResultCode()) {
case Activity.RESULT_OK:
console.log("給手機(jī)號(hào)碼: " + phone + " 發(fā)送短信成功");
console.log("發(fā)送的內(nèi)容: " + message);
break;
default:
console.log("給手機(jī)號(hào)碼: " + phone + " 發(fā)送短信成功");
break;
}
});
context.registerReceiver(sendMessage, new IntentFilter(SENT_SMS_ACTION));
//對(duì)方接收短信的監(jiān)聽(tīng)
let receiver = new BroadcastReceiver(function(ctx, intent) {
let phone = intent.getStringExtra("KEY_PHONENUM");
console.log("手機(jī)號(hào): " + phone + " 接收短信成功");
});
context.registerReceiver(receiver, new IntentFilter(DELIVERED_SMS_ACTION));
監(jiān)聽(tīng)的注冊(cè), 自己根據(jù)當(dāng)前手機(jī)所使用的的Autojs版本自己選擇,或者自己做成一個(gè)函數(shù)判斷后再注冊(cè).
快要下班了, 我還是直接通過(guò)整個(gè)腳本來(lái)講解其他的吧!
importPackage(android.content);
importClass(android.telephony.gsm.SmsManager);
importClass(android.app.PendingIntent);
importClass(android.app.Activity)
//監(jiān)聽(tīng)腳本的結(jié)束事件,結(jié)束后,注銷兩個(gè)監(jiān)聽(tīng),否則會(huì)一直再后臺(tái)監(jiān)聽(tīng)著...
events.on("exit", () => {
context.unregisterReceiver(receiver);
context.unregisterReceiver(sendMessage);
});
/**發(fā)送與接收的廣播**/
const SENT_SMS_ACTION = "SENT_SMS_ACTION";
const DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
//這里先注冊(cè)兩個(gè)監(jiān)聽(tīng),一個(gè)是發(fā)短信成功后的監(jiān)聽(tīng), 一個(gè)是對(duì)方接收短信成功
//的監(jiān)聽(tīng),短信發(fā)送失敗后的邏輯可以寫在這里.
let sendMessage = new BroadcastReceiver(function(ctx, intent) {
let phone = intent.getStringExtra("KEY_PHONENUM");
let message = intent.getStringExtra("KEY_MESSAGE");
switch (sendMessage.getResultCode()) {
case Activity.RESULT_OK:
console.log("給手機(jī)號(hào)碼: " + phone + " 發(fā)送短信成功");
console.log("發(fā)送的內(nèi)容: " + message);
break;
default:
console.log("給手機(jī)號(hào)碼: " + phone + " 發(fā)送短信成功");
break;
}
});
context.registerReceiver(sendMessage, new IntentFilter(SENT_SMS_ACTION));
let receiver = new BroadcastReceiver(function(ctx, intent) {
let phone = intent.getStringExtra("KEY_PHONENUM");
console.log("手機(jī)號(hào): " + phone + " 接收短信成功");
});
context.registerReceiver(receiver, new IntentFilter(DELIVERED_SMS_ACTION));
var phoneArray = ["電話號(hào)碼", "電話號(hào)碼"];
for (let i in phoneArray) {
sendMessages(phoneArray[i], "今天也是元?dú)鉂M滿的一天呢!!!", i);
sleep(100);
}
//保持腳本持續(xù)運(yùn)行,不會(huì)提前注銷監(jiān)聽(tīng)事件. 代碼執(zhí)行完自己手動(dòng)結(jié)束腳本.
//這里我沒(méi)優(yōu)化的更人性化一些...
while (true) {
// sendMessages("10010", "也是元?dú)鉂M滿的一天呢!!!");
sleep(60 * 1000);
}
/**
* 發(fā)短信的函數(shù)
* @param {string} phoneNumber 手機(jī)號(hào)碼
* @param {string} message 要發(fā)送的文字短信
* @param {int} rescode 短信廣播的標(biāo)識(shí),只要是個(gè)int類型的數(shù)字即可,每條短信使用不同的標(biāo)識(shí)號(hào),確保監(jiān)聽(tīng)可以正確識(shí)別
*/
function sendMessages(phoneNumber, message, rescode) {
let sms = SmsManager.getDefault();
let sentIntent = new Intent(SENT_SMS_ACTION);
sentIntent.putExtra("KEY_PHONENUM", phoneNumber);
sentIntent.putExtra("KEY_MESSAGE", message);
//這里需要說(shuō)一下: rescode是廣播標(biāo)識(shí),用來(lái)區(qū)分不同手機(jī)號(hào)的短信,
//PendingIntent.FLAG_ONE_SHOT 這個(gè)是說(shuō),該廣播只使用一次.再有相同的rescode標(biāo)識(shí)的廣播被接收到時(shí),不做處理.
let sentPI = PendingIntent.getBroadcast(context, rescode, sentIntent, PendingIntent.FLAG_ONE_SHOT);
// log("sentPI : " + sentPI);
let deliverIntent = new Intent(DELIVERED_SMS_ACTION);
deliverIntent.putExtra("KEY_PHONENUM", phoneNumber);
let deliverPI = PendingIntent.getBroadcast(context, rescode, deliverIntent, PendingIntent.FLAG_ONE_SHOT);
// log("deliverPI : " + deliverPI);
/**
* 這里是判斷短信內(nèi)容如果大于70個(gè)字, 就需要分兩條或者多條短信發(fā)送出去.
*/
if (message.length > 70) {
let msgs = sms.divideMessage(message);
for (let i in msgs) {
let msg = msgs[i]
sms.sendTextMessage(phoneNumber, null, msg, sentPI, deliverPI);
}
} else {
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliverPI);
}
}
好了下班前, 把這個(gè)折騰好幾晚上的問(wèn)題.搞定. 希望可以幫助更多的人. 折騰不易. 希望得到幫助的小哥哥們,打賞一下. 謝謝啦...
-
版本
+關(guān)注
關(guān)注
0文章
15瀏覽量
9472 -
監(jiān)聽(tīng)
+關(guān)注
關(guān)注
0文章
22瀏覽量
8325 -
注冊(cè)
+關(guān)注
關(guān)注
2文章
7瀏覽量
11247
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論