简体   繁体   English

如何在Android中注销广播接收器

[英]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. 在名称为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. 该方法是从我的服务类的onCreate方法调用的。 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! 现在,我希望在服务完成时,我想使用onDestroy方法取消注册广泛的广播接收器。我该如何实现...请帮助我解决此问题。谢谢!

You can call unregisterReceiver (BroadcastReceiver receiver) to unregister the broadcast receivers. 您可以调用unregisterReceiver(BroadcastReceiver接收器)以注销广播接收器。 The context you have received as part of sendSMS should be stored and then later the same can be used to unregister the receiver. 应该存储已作为sendSMS一部分收到的上下文,然后可以使用该上下文注销接收者。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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