Android - 如何从其他类注销registerReceiver

时间:2016-11-28 12:14:32

标签: android broadcastreceiver

我的问题在这里我从registedReceiver泄露了。我搜索了关于如何关闭registedReceiver的任何说明,他们指示你应该将它关闭到onPause或ondestroy但我找不到关于我如何关闭我的任何内容。

我的代码

public class SMSHandler{    

    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    public SMSHandler(Context context, String phoneNum, String message){
        sendSMS(context,phoneNum,message);
    }

    private void sendSMS(Context context,String phoneNum, String message) {

        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(
                SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
                new Intent(DELIVERED), 0);

        // when the sms has been sent
        context.registerReceiver(new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                switch(getResultCode()){
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(context, "Generic Failure", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(context, "No Service", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        },new IntentFilter(SENT));

        context.registerReceiver(new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                switch(getResultCode()){
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(context, "SMS note delivered", Toast.LENGTH_SHORT).show();
                    break;
                }
            }

        }, new IntentFilter(DELIVERED));

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNum, null, message, sentPI, deliveredPI);
    }
}

2 个答案:

答案 0 :(得分:0)

这是您注册和取消注册接收器的方式......

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      //Your on receive code here...
    }
};

@Override
public void onDestroy() {
super.onDestroy();
RTLog.d(TAG, "Destroy Called");
//unregister the receiver
getActivity().unregisterReceiver(broadcastReceiver);

}

答案 1 :(得分:0)

您正在匿名使用BroadCastReceiver,没有特定对象引用这些类。在另一个类中使用它们并在此处使用Object,您可以从任何地方访问它。