简体   繁体   中英

How to unregister the broadcast receiver in android

Is it necessary to unregister the broad cast receiver? If so then how can I unregister it. My code scenario is as follows:

I have make a static method to send an sms in which I have registered a broadcast receiver like that:

    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);        
    } 

This method is created in a non activity class whose name is HomeSafeStaticMethod. Now what I do I am calling this method in my service to send the sms like that:-

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

This method is calling from onCreate method of my service class. Now I want that when my service will finished I want to unregister the broad cast receiver in onDestroy method.How can I achieve that... Please help me to sort out this problem.Thanks in advance!

You can call unregisterReceiver (BroadcastReceiver receiver) to unregister the broadcast receivers. The context you have received as part of sendSMS should be stored and then later the same can be used to unregister the receiver.

EDIT:

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;
}

只需编写以下代码

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM