如何取消注册android中的广播接收器

时间:2013-08-02 10:46:59

标签: android broadcastreceiver

是否有必要取消注册广播接收器?如果是,那么我该如何取消注册呢。 我的代码场景如下:

我已经制作了一个静态方法来发送短信,其中我注册了一个广播接收器:

    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的非活动类中创建。现在我做什么我在我的服务中调用这个方法来发送这样的短信: -

HomeSafeStaticMethod.sendSMS(CheckInSOSMessageService.this,emergencyNumber,first_Name+"\n"+address+"  "+city+"  "+country); 

此方法从我的服务类的onCreate方法调用。现在我希望当我的服务完成时我想取消注册onDestroy方法中的广播接收器。我怎样才能实现...请帮助我解决这个问题。谢谢你提前!

2 个答案:

答案 0 :(得分:0)

你可以打电话 unregisterReceiver (BroadcastReceiver receiver)取消注册广播接收器。您应该存储作为sendSMS的一部分收到的上下文,然后可以使用相同的方式取消注册接收方。

修改

private static Context mContext = null;

static BroadcastReceiver mSentReceiver = 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;
        }
    }

};

static BroadcastReceiver mDeleiveredReceiver = 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;                        
        }
    }
};

public static void sendSMS(final Context context,String phoneNumber, String message) {
    if(mContext == null) {
        mContext = context;
        mContext.registerReceiver(mSentReceiver, new IntentFilter(SENT));
        mContext.registerReceiver(mDeleiveredReceiver, new IntentFilter(DELIVERED));
    }

    // rest of your code ....
}

// this method should be called whenever you want to remove the receivers
public static void removeReceivers() {
    mContext.unregisterReceiver(mSentReceiver);
    mContext.unregisterReceiver(mDeleiveredReceiver);
    mContext = null;
}

答案 1 :(得分:0)

只需编写以下代码

即可
PendingIntent.getBroadcast(this, 0, new Intent(this, BroadCastReceiverClass.class), PendingIntent.FLAG_UPDATE_CURRENT).cancel();