My app need to receiver broadcast and do something just once. The occasion might be the time my app has not started yet so I declare the receiver in the Manifest:
<receiver
android:name=".OneShotBroadcast"
android:permission="com.ytinrete.broadcast.accesspermisson">
<intent-filter>
<action android:name="com.ytinrete.broadcast.oneshot"/>
</intent-filter>
</receiver>
And here's onReceive:
@Override
public void onReceive(Context context, Intent intent) {
//want to doing my work here just once
......
}
Here comes the problem: I can't find any ways to unregister this receiver when I first get the broadcast.
I've try this:
context.unregisterReceiver(this);
But It raise the exception:
java.lang.RuntimeException: Unable to start receiver com.ytinrete.OneShotBroadcast:
java.lang.IllegalArgumentException: Receiver not registered: com.ytinrete.OneShotBroadcast@5354a224
Currently I just set a static boolean as a flag in the receiver and it works, but I tend to find a way to unregister so that I don't need to maintain such magic variable in my project.
Is there another way to work around? Any clues will be appreciated!
Thanks to Truong Phu Quoc There's really a way to unregister: Android - how to unregister a receiver created in the manifest I've tested it and it works.