Recentemente ho fatto una domanda sul controllo dello stato di un SMS inviato e la risposta fornita era uno snippet di codice che registrava due "anonimi interni" (correggi la mia terminologia se è errata BroadcastReceivers
per ascoltare le trasmissioni inviate/consegnate via SMS. Questi ricevitori dovevano solo ricevere i dati relativi agli SMS che la mia applicazione aveva appena inviato, quindi non era necessario ascoltarli in modo permanente.Devo annullare la registrazione di "anonymous" BroadcastReceiver
Il mio pensiero immediato è stato "bene, dovrò annullarli dopo averli terminati", ma è corretto? Ho chiesto questo poster perché non aveva incluso alcun codice di annullamento della registrazione, ma non ho ricevuto risposta. Il codice sembra essere un modo abbastanza standard per fare ciò che voglio in quanto appare su numerosi siti di sviluppo Android. Eccolo:
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
Il codice funziona correttamente.
Inoltre, non viene notificato alcun evento inviato/recapitato tramite SMS esterno alla mia app. Per esempio. Posso inviare un SMS dopo che questi BroadcastReceivers
sono stati registrati e non vedo alcun messaggio Toast
.
Così, ho due domande:
- Ho bisogno di annullare la registrazione di questi
BroadcastReceivers
? - In caso contrario, perché no?
Perché pensi che dovrebbe andare in crash? – barry
Aggiornato la mia risposta. –
Bene, il codice è in un BroadcastReceiver diverso (riattivato da AlarmManager) quindi non ho alcun metodo di ciclo di vita per annullare la registrazione. Se necessario, annulliamo la registrazione di onReceive() – barry