class CtTask extends AsyncTask<String, String, Void> {
String SENT_SMS_FLAG = "SENT_SMS";
String DELIVER_SMS_FLAG = "DELIVER_SMS";
PendingIntent sentIn;
PendingIntent deliverIn;
BroadcastReceiver sentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent in) {
switch (getResultCode()) {
case Activity.RESULT_OK:
// sent SMS message successfully;
Toast toast = Toast.makeText(getApplicationContext(),
"Сообщение отправлено!", Toast.LENGTH_SHORT);
toast.show();
break;
default:
// sent SMS message failed
break;
}
}
};
BroadcastReceiver deliverReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent in) {
// SMS delivered actions
switch (getResultCode()) {
case Activity.RESULT_OK:
// sent SMS message successfully;
Toast toast = Toast.makeText(getApplicationContext(),
"Сообщение доставлено!", Toast.LENGTH_SHORT);
toast.show();
break;
default:
// sent SMS message failed
break;
}
}
};
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "onPreExecute()", Toast.LENGTH_LONG).show();
Intent sentIn = new Intent(SENT_SMS_FLAG);
final PendingIntent sentPIn = PendingIntent.getBroadcast(getApplicationContext(),
0,
sentIn, 0);
Intent deliverIn = new Intent(SENT_SMS_FLAG);
final PendingIntent deliverPIn = PendingIntent.getBroadcast(getApplicationContext(), 0,
deliverIn, 0);
registerReceiver(sentReceiver, new IntentFilter(SENT_SMS_FLAG));
registerReceiver(deliverReceiver, new IntentFilter(DELIVER_SMS_FLAG));
}
@Override
protected Void doInBackground(String... urls) {
try {
for (String url : urls) {
send(url);
publishProgress(url);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), "onPostExecute()", Toast.LENGTH_LONG).show();
unregisterReceiver(sentReceiver);
unregisterReceiver(deliverReceiver);
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Toast.makeText(getApplicationContext(), "progress: "+values[0], Toast.LENGTH_LONG).show();
}
private String send(String number) throws InterruptedException {
smsManager.sendTextMessage(number, null, "text", sentIn, deliverIn);
return "ok";
}
}
ArrayList numbers = new ArrayList();
SmsManager smsManager = SmsManager.getDefault();
String[] f = (String[]) numbers.toArray(new String[0]);
CtTask catTask = new CatTask();
ctTask.execute(f);
Вопрос: как правильно использовать Broadcast в классе AsyncTask?
AsyncTask
тут же прибивается системой, с выполнением метода onPostExecute
, в котором ваши broadcast receivers отвязываются от broadcast manager'a. А так как широковещательное сообщение может прийти в BroadcastReceiver
с задержкой, то у вас и получается, что AsyncTask
отработал, broadcast receivers отвязались, сообщение пришло, но обработать его некому.Activity
, и отключайте их в onPause()
. protected void onResume() {
super.onResume();
registerReceiver(sentReceiver, new IntentFilter(SENT_SMS_FLAG));
registerReceiver(deliverReceiver, new IntentFilter(DELIVER_SMS_FLAG));
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(sentReceiver);
unregisterReceiver(deliverReceiver);
}
BroadcastReceiver sentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent in) {
switch (getResultCode()) {
case Activity.RESULT_OK:
// sent SMS message successfully;
Toast toast = Toast.makeText(getApplicationContext(),
"Сообщение отправлено!", Toast.LENGTH_SHORT);
toast.show();
break;
default:
// sent SMS message failed
break;
}
}
};
BroadcastReceiver deliverReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent in) {
// SMS delivered actions
switch (getResultCode()) {
case Activity.RESULT_OK:
// sent SMS message successfully;
Toast toast = Toast.makeText(getApplicationContext(),
"Сообщение доставлено!", Toast.LENGTH_SHORT);
toast.show();
break;
default:
// sent SMS message failed
break;
}
}
};
Intent sentIn = new Intent(SENT_SMS_FLAG);
Intent deliverIn = new Intent(SENT_SMS_FLAG);
private String send(String number) throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
final PendingIntent deliverPIn = PendingIntent.getBroadcast(getApplicationContext(), 0, deliverIn, 0);
final PendingIntent sentPIn = PendingIntent.getBroadcast(getApplicationContext(), 0,sentIn, 0);
smsManager.sendTextMessage(number, null, "text", sentPIn, deliverPIn);
return "ok";
}