放送受信機の登録を解除する必要はありますか? もしそうなら、どうすればそれを登録解除できますか。私のコードシナリオは次のとおりです。
そのようなブロードキャストレシーバーを登録したSMSを送信する静的メソッドを作成しました:
public static void sendSMS(final Context mContext,String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
mContext.registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(mContext, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(mContext, "No cellular network service Available",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(mContext, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(mContext, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
mContext.registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(mContext, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
このメソッドは、HomeSafeStaticMethod という名前の非アクティビティ クラスで作成されます。今、私は自分のサービスでこのメソッドを呼び出して、そのようなSMSを送信しています:-
HomeSafeStaticMethod.sendSMS(CheckInSOSMessageService.this,emergencyNumber,first_Name+"\n"+address+" "+city+" "+country);
このメソッドは、サービス クラスの onCreate メソッドから呼び出しています。サービスが終了したら、onDestroy メソッドでブロードキャスト レシーバーの登録を解除したいと考えています。どうすればそれを達成できますか... この問題を解決するのを手伝ってください。よろしくお願いします!