Unregister Broadcast Receiver using another Broadcast Receiver
I am working on an android app in which I am registering Broadcast Receiver in one of the activity.
Problem
Lets assume that activity name is MyActivity and the Broadcast Receiver I register in this activity is myReceiver.
I want to unregister myReceiver from another Broadcast Receiver's, lets call it secondReceiver, onReceive method. I have a method, lets call it myMethod, to unregister the myReceiver inside myActivity activity.
When I call this myMethod from inside the secondReceiver, I get the IntentReceiverLeaked exception.
Question
How can I unregister Broadcast Receiver from another Broadcast Receiver?
add a comment |
I am working on an android app in which I am registering Broadcast Receiver in one of the activity.
Problem
Lets assume that activity name is MyActivity and the Broadcast Receiver I register in this activity is myReceiver.
I want to unregister myReceiver from another Broadcast Receiver's, lets call it secondReceiver, onReceive method. I have a method, lets call it myMethod, to unregister the myReceiver inside myActivity activity.
When I call this myMethod from inside the secondReceiver, I get the IntentReceiverLeaked exception.
Question
How can I unregister Broadcast Receiver from another Broadcast Receiver?
Try to register/unregister the BroadcastReceiver with application Context instead
– Demonick
Jan 2 at 7:54
@Demonick it works, thank you.
– Nick
Jan 2 at 10:38
add a comment |
I am working on an android app in which I am registering Broadcast Receiver in one of the activity.
Problem
Lets assume that activity name is MyActivity and the Broadcast Receiver I register in this activity is myReceiver.
I want to unregister myReceiver from another Broadcast Receiver's, lets call it secondReceiver, onReceive method. I have a method, lets call it myMethod, to unregister the myReceiver inside myActivity activity.
When I call this myMethod from inside the secondReceiver, I get the IntentReceiverLeaked exception.
Question
How can I unregister Broadcast Receiver from another Broadcast Receiver?
I am working on an android app in which I am registering Broadcast Receiver in one of the activity.
Problem
Lets assume that activity name is MyActivity and the Broadcast Receiver I register in this activity is myReceiver.
I want to unregister myReceiver from another Broadcast Receiver's, lets call it secondReceiver, onReceive method. I have a method, lets call it myMethod, to unregister the myReceiver inside myActivity activity.
When I call this myMethod from inside the secondReceiver, I get the IntentReceiverLeaked exception.
Question
How can I unregister Broadcast Receiver from another Broadcast Receiver?
edited Jan 2 at 8:30
DemiDust
13911
13911
asked Jan 2 at 7:47
NickNick
3261417
3261417
Try to register/unregister the BroadcastReceiver with application Context instead
– Demonick
Jan 2 at 7:54
@Demonick it works, thank you.
– Nick
Jan 2 at 10:38
add a comment |
Try to register/unregister the BroadcastReceiver with application Context instead
– Demonick
Jan 2 at 7:54
@Demonick it works, thank you.
– Nick
Jan 2 at 10:38
Try to register/unregister the BroadcastReceiver with application Context instead
– Demonick
Jan 2 at 7:54
Try to register/unregister the BroadcastReceiver with application Context instead
– Demonick
Jan 2 at 7:54
@Demonick it works, thank you.
– Nick
Jan 2 at 10:38
@Demonick it works, thank you.
– Nick
Jan 2 at 10:38
add a comment |
1 Answer
1
active
oldest
votes
One way to run your unregister method which is myMethod from another activity instance is to register a broadcast receiver on your myActivity with a custom action of your own, and in the onReceive method you call myMethod to unregister your reciever. Then you just need to send a broadcast with your declared action when you want to unregister the receiver . This way there won't be any leak due to not maintaining a reference to myActivity.
Here's a demonstration:
public class MyActivity extends AppCompatActivity {
private static final String ACTION_UNREGISTER = "com.yourDomain.subDomain.UNREGISTER_RECEIVER";
private BroadcastReceiver mUnregisteringReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myMethod();
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
...
registerReceiver(mUnregisteringReceiver, new IntentFilter(ACTION_UNREGISTER));
}
@Override
protected void onDestroy() {
...
unregisterReceiver(mUnregisteringReceiver);
}
}
what IfMyActivityis destroyed? ThenmUnregisteringReceiverwill get unregistered andmyMethodwon't be called and hence the intent leak exception.
– Nick
Jan 2 at 8:46
@Nick the proper way of registering a context-registered receiver on your activity is to unregister it when it gets destroyed, because context-registered receivers are bound to activity lifetime and by unregistering it you make sure no leak will occur, as Android documentation says. The only case that would apply here is that you want to unregister your receiver before the activity gets destroyed to prevent receiving further broadcasts which in that case your activity is still alive.
– Sdghasemi
Jan 2 at 8:56
actually, I am downloading a file from server in background using Intent service, I want to be able to download and execute some code when download completes even when activity is destroyed.
– Nick
Jan 2 at 9:08
In that case you should use Android PendingIntent passing your activity class with some extras you need; which is designed for the exact use you need. In simple words it keeps a pending context to your component and runs it the moment you need it (end of download) which will open your activity while passing the extras so it can handle the proper action you need. A simple example would be when you click on a notification and your activity gets opened.
– Sdghasemi
Jan 2 at 9:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002886%2funregister-broadcast-receiver-using-another-broadcast-receiver%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
One way to run your unregister method which is myMethod from another activity instance is to register a broadcast receiver on your myActivity with a custom action of your own, and in the onReceive method you call myMethod to unregister your reciever. Then you just need to send a broadcast with your declared action when you want to unregister the receiver . This way there won't be any leak due to not maintaining a reference to myActivity.
Here's a demonstration:
public class MyActivity extends AppCompatActivity {
private static final String ACTION_UNREGISTER = "com.yourDomain.subDomain.UNREGISTER_RECEIVER";
private BroadcastReceiver mUnregisteringReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myMethod();
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
...
registerReceiver(mUnregisteringReceiver, new IntentFilter(ACTION_UNREGISTER));
}
@Override
protected void onDestroy() {
...
unregisterReceiver(mUnregisteringReceiver);
}
}
what IfMyActivityis destroyed? ThenmUnregisteringReceiverwill get unregistered andmyMethodwon't be called and hence the intent leak exception.
– Nick
Jan 2 at 8:46
@Nick the proper way of registering a context-registered receiver on your activity is to unregister it when it gets destroyed, because context-registered receivers are bound to activity lifetime and by unregistering it you make sure no leak will occur, as Android documentation says. The only case that would apply here is that you want to unregister your receiver before the activity gets destroyed to prevent receiving further broadcasts which in that case your activity is still alive.
– Sdghasemi
Jan 2 at 8:56
actually, I am downloading a file from server in background using Intent service, I want to be able to download and execute some code when download completes even when activity is destroyed.
– Nick
Jan 2 at 9:08
In that case you should use Android PendingIntent passing your activity class with some extras you need; which is designed for the exact use you need. In simple words it keeps a pending context to your component and runs it the moment you need it (end of download) which will open your activity while passing the extras so it can handle the proper action you need. A simple example would be when you click on a notification and your activity gets opened.
– Sdghasemi
Jan 2 at 9:20
add a comment |
One way to run your unregister method which is myMethod from another activity instance is to register a broadcast receiver on your myActivity with a custom action of your own, and in the onReceive method you call myMethod to unregister your reciever. Then you just need to send a broadcast with your declared action when you want to unregister the receiver . This way there won't be any leak due to not maintaining a reference to myActivity.
Here's a demonstration:
public class MyActivity extends AppCompatActivity {
private static final String ACTION_UNREGISTER = "com.yourDomain.subDomain.UNREGISTER_RECEIVER";
private BroadcastReceiver mUnregisteringReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myMethod();
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
...
registerReceiver(mUnregisteringReceiver, new IntentFilter(ACTION_UNREGISTER));
}
@Override
protected void onDestroy() {
...
unregisterReceiver(mUnregisteringReceiver);
}
}
what IfMyActivityis destroyed? ThenmUnregisteringReceiverwill get unregistered andmyMethodwon't be called and hence the intent leak exception.
– Nick
Jan 2 at 8:46
@Nick the proper way of registering a context-registered receiver on your activity is to unregister it when it gets destroyed, because context-registered receivers are bound to activity lifetime and by unregistering it you make sure no leak will occur, as Android documentation says. The only case that would apply here is that you want to unregister your receiver before the activity gets destroyed to prevent receiving further broadcasts which in that case your activity is still alive.
– Sdghasemi
Jan 2 at 8:56
actually, I am downloading a file from server in background using Intent service, I want to be able to download and execute some code when download completes even when activity is destroyed.
– Nick
Jan 2 at 9:08
In that case you should use Android PendingIntent passing your activity class with some extras you need; which is designed for the exact use you need. In simple words it keeps a pending context to your component and runs it the moment you need it (end of download) which will open your activity while passing the extras so it can handle the proper action you need. A simple example would be when you click on a notification and your activity gets opened.
– Sdghasemi
Jan 2 at 9:20
add a comment |
One way to run your unregister method which is myMethod from another activity instance is to register a broadcast receiver on your myActivity with a custom action of your own, and in the onReceive method you call myMethod to unregister your reciever. Then you just need to send a broadcast with your declared action when you want to unregister the receiver . This way there won't be any leak due to not maintaining a reference to myActivity.
Here's a demonstration:
public class MyActivity extends AppCompatActivity {
private static final String ACTION_UNREGISTER = "com.yourDomain.subDomain.UNREGISTER_RECEIVER";
private BroadcastReceiver mUnregisteringReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myMethod();
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
...
registerReceiver(mUnregisteringReceiver, new IntentFilter(ACTION_UNREGISTER));
}
@Override
protected void onDestroy() {
...
unregisterReceiver(mUnregisteringReceiver);
}
}
One way to run your unregister method which is myMethod from another activity instance is to register a broadcast receiver on your myActivity with a custom action of your own, and in the onReceive method you call myMethod to unregister your reciever. Then you just need to send a broadcast with your declared action when you want to unregister the receiver . This way there won't be any leak due to not maintaining a reference to myActivity.
Here's a demonstration:
public class MyActivity extends AppCompatActivity {
private static final String ACTION_UNREGISTER = "com.yourDomain.subDomain.UNREGISTER_RECEIVER";
private BroadcastReceiver mUnregisteringReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myMethod();
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
...
registerReceiver(mUnregisteringReceiver, new IntentFilter(ACTION_UNREGISTER));
}
@Override
protected void onDestroy() {
...
unregisterReceiver(mUnregisteringReceiver);
}
}
edited Jan 2 at 8:19
answered Jan 2 at 8:12
SdghasemiSdghasemi
1,3951220
1,3951220
what IfMyActivityis destroyed? ThenmUnregisteringReceiverwill get unregistered andmyMethodwon't be called and hence the intent leak exception.
– Nick
Jan 2 at 8:46
@Nick the proper way of registering a context-registered receiver on your activity is to unregister it when it gets destroyed, because context-registered receivers are bound to activity lifetime and by unregistering it you make sure no leak will occur, as Android documentation says. The only case that would apply here is that you want to unregister your receiver before the activity gets destroyed to prevent receiving further broadcasts which in that case your activity is still alive.
– Sdghasemi
Jan 2 at 8:56
actually, I am downloading a file from server in background using Intent service, I want to be able to download and execute some code when download completes even when activity is destroyed.
– Nick
Jan 2 at 9:08
In that case you should use Android PendingIntent passing your activity class with some extras you need; which is designed for the exact use you need. In simple words it keeps a pending context to your component and runs it the moment you need it (end of download) which will open your activity while passing the extras so it can handle the proper action you need. A simple example would be when you click on a notification and your activity gets opened.
– Sdghasemi
Jan 2 at 9:20
add a comment |
what IfMyActivityis destroyed? ThenmUnregisteringReceiverwill get unregistered andmyMethodwon't be called and hence the intent leak exception.
– Nick
Jan 2 at 8:46
@Nick the proper way of registering a context-registered receiver on your activity is to unregister it when it gets destroyed, because context-registered receivers are bound to activity lifetime and by unregistering it you make sure no leak will occur, as Android documentation says. The only case that would apply here is that you want to unregister your receiver before the activity gets destroyed to prevent receiving further broadcasts which in that case your activity is still alive.
– Sdghasemi
Jan 2 at 8:56
actually, I am downloading a file from server in background using Intent service, I want to be able to download and execute some code when download completes even when activity is destroyed.
– Nick
Jan 2 at 9:08
In that case you should use Android PendingIntent passing your activity class with some extras you need; which is designed for the exact use you need. In simple words it keeps a pending context to your component and runs it the moment you need it (end of download) which will open your activity while passing the extras so it can handle the proper action you need. A simple example would be when you click on a notification and your activity gets opened.
– Sdghasemi
Jan 2 at 9:20
what If
MyActivity is destroyed? Then mUnregisteringReceiver will get unregistered and myMethod won't be called and hence the intent leak exception.– Nick
Jan 2 at 8:46
what If
MyActivity is destroyed? Then mUnregisteringReceiver will get unregistered and myMethod won't be called and hence the intent leak exception.– Nick
Jan 2 at 8:46
@Nick the proper way of registering a context-registered receiver on your activity is to unregister it when it gets destroyed, because context-registered receivers are bound to activity lifetime and by unregistering it you make sure no leak will occur, as Android documentation says. The only case that would apply here is that you want to unregister your receiver before the activity gets destroyed to prevent receiving further broadcasts which in that case your activity is still alive.
– Sdghasemi
Jan 2 at 8:56
@Nick the proper way of registering a context-registered receiver on your activity is to unregister it when it gets destroyed, because context-registered receivers are bound to activity lifetime and by unregistering it you make sure no leak will occur, as Android documentation says. The only case that would apply here is that you want to unregister your receiver before the activity gets destroyed to prevent receiving further broadcasts which in that case your activity is still alive.
– Sdghasemi
Jan 2 at 8:56
actually, I am downloading a file from server in background using Intent service, I want to be able to download and execute some code when download completes even when activity is destroyed.
– Nick
Jan 2 at 9:08
actually, I am downloading a file from server in background using Intent service, I want to be able to download and execute some code when download completes even when activity is destroyed.
– Nick
Jan 2 at 9:08
In that case you should use Android PendingIntent passing your activity class with some extras you need; which is designed for the exact use you need. In simple words it keeps a pending context to your component and runs it the moment you need it (end of download) which will open your activity while passing the extras so it can handle the proper action you need. A simple example would be when you click on a notification and your activity gets opened.
– Sdghasemi
Jan 2 at 9:20
In that case you should use Android PendingIntent passing your activity class with some extras you need; which is designed for the exact use you need. In simple words it keeps a pending context to your component and runs it the moment you need it (end of download) which will open your activity while passing the extras so it can handle the proper action you need. A simple example would be when you click on a notification and your activity gets opened.
– Sdghasemi
Jan 2 at 9:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002886%2funregister-broadcast-receiver-using-another-broadcast-receiver%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try to register/unregister the BroadcastReceiver with application Context instead
– Demonick
Jan 2 at 7:54
@Demonick it works, thank you.
– Nick
Jan 2 at 10:38