我需要发送短信列表。因此,我已经创建了一个前台服务,用于每隔x秒检查一次新的短信,以使其与后台线程一起发送,以免冻结ui线程。 但是,当我执行“ for loop”时,我的用户界面冻结了,我没有说什么:(
我尝试创建处理程序而不是我的Subject,但是选择的是完全相同的东西
感谢您的帮助!
前台服务:
...
public void onCreate() {
super.onCreate();
//Toast.makeText(getApplicationContext(), "CFCFCFGCG", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// On démarre le service, et on le garde actif
//TODO do something useful
string_url_get = intent.getExtras().getString("url_get");
string_url_post = intent.getExtras().getString("url_post");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Bot SMS Rosace")
.setContentText("Bot en cours de fonctionnement...")
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
Thread background = new Thread(new Runnable() {
Bundle messageBundle = new Bundle();
Message myMessage;
public void run() {
try {
while (isRunning.get()) {
//Si on arrete le service
if(sendingMessage == false) {
jsonRequest();
sendingMessage = true;
}
else {
sleep(20000);
}
}
} catch (Throwable t) {
Log.i("CHECK", "ERRRREEEUUURRRR" + t.toString());
// gérer l'exception et arrêter le traitement
}
}
});
isRunning.set(true);
isPausing.set(false);
background.start();
//Toast.makeText(getApplicationContext(), "COUCOU", Toast.LENGTH_SHORT).show();
return Service.START_REDELIVER_INTENT;
}
...
jsonRequest():
...
for (int i = 0; i < Integer.parseInt(nb_Sms); ++i) {
//int temp = i + 1;
//textLog.setText("\n#[" + formatTime() + "] - Envoie SMS n°" + Integer.toString(temp) + textLog.getText().toString());
//On récupère l'objet SMS
final JSONObject smsObject = (JSONObject) response_Array.get(i);
//On conçu un handler pour pouvoir espacer les envoies
SMS sms = new SMS();
sms.createSMS(smsObject);
sendLongSmsMessage4(getApplicationContext(), sms);
//temp = Integer.parseInt(nb_Sms) - temp;
//textLog.setText("\n#[" + formatTime() + "] - Envoie Terminé !" + textLog.getText().toString());
//textLog.setText("\n#[" + formatTime() + "] - Nombre de SMS restant: " + temp + textLog.getText().toString());
}
...
sendLongSmsMessage4():
private void sendLongSmsMessage4(Context context, final SMS messageInfo) {
//On vérifie l'envoie pour chaque partie du SMS
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// On vérifie que toutes les parties sont envoyé pour déclaré le message envoyer
switch (getResultCode()) {
case Activity.RESULT_OK:
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
messageInfo.fail("Error - Generic failure");
int errorCode = intent.getIntExtra("errorCode", -1);
if (errorCode != -1) {
messageInfo.setGenericFailureCode(errorCode);
}
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
messageInfo.fail("Error - No Service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
messageInfo.fail("Error - Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
messageInfo.fail("Error - Radio off");
break;
}
nMsgParts--;
if (nMsgParts <= 0) {
// Stop us from getting any other broadcasts (may be for other messages)
Log.i("CHECK", "All message part responses received, unregistering message Id: " + messageInfo.getSMSId() + "NOMBRE SMS RESTANT: " + nbSMSforkill + "Isfailed ?" + messageInfo.isFailed());
context.unregisterReceiver(this);
if (messageInfo.isFailed()) {
Log.i("CHECK", "SMS Failure for message id: " + messageInfo.getSMSId());
Log.i("CHECK", "Reason: " + messageInfo.getFailInfo());
Log.i("CHECK", "ErrorCode: " + messageInfo.getGenericFailureCode());
} else {
Log.i("CHECK", "SMS Success for message id: " + messageInfo.getSMSId());
messageInfo.setSent();
}
nbSMSforkill--;
if(nbSMSforkill == 0){
Log.i("CHECK", "PARRER A ENVOYER SMS");
sendingMessage = false;
}
}
}
};
context.registerReceiver(broadcastReceiver, new IntentFilter("SENT" + messageInfo.getSMSId()));
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> messageParts = smsManager.divideMessage(messageInfo.getSMSId());
ArrayList<PendingIntent> pendingIntents = new ArrayList<>(messageParts.size());
nMsgParts = messageParts.size();
for (int i = 0; i < messageParts.size(); i++) {
Intent sentIntent = new Intent("SENT" + messageInfo.getSMSId());
pendingIntents.add(PendingIntent.getBroadcast(context, 0, sentIntent, 0));
}
Log.i("CHECK", "About to send multi-part message Id: " + messageInfo.getSMSId());
smsManager.sendMultipartTextMessage("00", null, messageParts, pendingIntents, null);
}
启动服务代码:
...
intent = new Intent(getApplicationContext(), SMSService.class);
intent.putExtra("url_get", string_url_get);
intent.putExtra("url_post", string_url_send);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
startService(intent);
...