Answer the question
In order to leave comments, you need to log in
Android: how to catch Broadcasts in AsyncTask?
Hello! There is very little experience in androids, I decided to try asking here,
There is a class for async:
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);
Answer the question
In order to leave comments, you need to log in
Question: how to properly use Broadcast in the AsyncTask class?
AsyncTask
immediately beaten by the system, with the execution of the method onPostExecute
in which your broadcast receivers are untied from the broadcast manager'a. And since the broadcast message may arrive BroadcastReceiver
with a delay, then it turns out that it AsyncTask
worked, the broadcast receivers got rid of, the message arrived, but there is no one to process it. Activity
, and disable them in onPause()
.
Ok, I'll try
UPD:
1. I register a BroadcastReceiver in an activity
2. In onPause() I cancel in the same activity
3. In an asynchronous class, in onPreExecute() I create an intent
Something like this? Anyway, something is not right ...
UPD2: It seems to have thought of it, it works on the emulator. Principle:
1. In the main activity:
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";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question