下面列出了android.support.v4.content.LocalBroadcastManager#unregisterReceiver ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(timeout = 5000)
public void testStart() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
BroadcastReceiver serviceBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
int action = bundle.getInt("action");
if (action == LocationServiceImpl.MSG_ON_SERVICE_STARTED) {
latch.countDown();
}
}
};
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(InstrumentationRegistry.getTargetContext());
lbm.registerReceiver(serviceBroadcastReceiver, new IntentFilter(LocationServiceImpl.ACTION_BROADCAST));
proxy.start();
latch.await();
lbm.unregisterReceiver(serviceBroadcastReceiver);
}
@Test(timeout = 5000)
public void testStop() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
BroadcastReceiver serviceBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
int action = bundle.getInt("action");
if (action == LocationServiceImpl.MSG_ON_SERVICE_STOPPED) {
latch.countDown();
}
}
};
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(InstrumentationRegistry.getTargetContext());
lbm.registerReceiver(serviceBroadcastReceiver, new IntentFilter(LocationServiceImpl.ACTION_BROADCAST));
proxy.start();
proxy.stop();
latch.await();
lbm.unregisterReceiver(serviceBroadcastReceiver);
}
private void tearDownObjectAssociations() {
if (broadcastReceiver != null) {
LocalBroadcastManager localBroadcastManager =
LocalBroadcastManager.getInstance(getContext());
localBroadcastManager.unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
// If we were already waiting on a controller to be given back, make sure we aren't waiting
// anymore. Otherwise when that controller is given back to the callback, it will go and
// register a broadcast receiver for it.
if (creationCallback != null) {
creationCallback.cancel();
creationCallback = null;
}
likeActionController = null;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (outState != null)
outState.putParcelable(BUNDLE_READ_POSITION_CONFIG_CHANGE, lastReadPosition);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.unregisterReceiver(searchReceiver);
localBroadcastManager.unregisterReceiver(closeBroadcastReceiver);
if (r2StreamerServer != null) {
r2StreamerServer.stop();
}
if (isFinishing())
localBroadcastManager.sendBroadcast(new Intent(FolioReader.ACTION_FOLIOREADER_CLOSED));
}
@Override
public void onPause() {
super.onPause();
// Unregister the receiver
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.unregisterReceiver(receiver);
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
getActivity());
localBroadcastManager.unregisterReceiver(serviceUpdateBroadcastReceiver);
localBroadcastManager.unregisterReceiver(runnerUpdateBroadcastReceiver);
getActivity().unbindService(bulkReadCardsServiceConnection);
}
@Override
public void onDestroy() {
//lifecycle events
SoundPoolManager.getInstance(cordova.getActivity()).release();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(cordova.getActivity());
lbm.unregisterReceiver(mBroadcastReceiver);
super.onDestroy();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("PopUpActivity", "onDestroy()");
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(new Intent(INTENT_ACTION_DESTROYED));
localBroadcastManager.unregisterReceiver(broadcastReceiver);
releaseViews();
}
public void testSimpleCall() throws InterruptedException {
AppEventsLogger.setFlushBehavior(AppEventsLogger.FlushBehavior.EXPLICIT_ONLY);
TestSession session1 = TestSession.createSessionWithSharedUser(getActivity(), null);
TestSession session2 = TestSession.createSessionWithSharedUser(getActivity(), null, SECOND_TEST_USER_TAG);
AppEventsLogger logger1 = AppEventsLogger.newLogger(getActivity(), session1);
AppEventsLogger logger2 = AppEventsLogger.newLogger(getActivity(), session2);
final WaitForBroadcastReceiver waitForBroadcastReceiver = new WaitForBroadcastReceiver();
waitForBroadcastReceiver.incrementExpectCount();
final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
// Need to get notifications on another thread so we can wait for them.
runOnBlockerThread(new Runnable() {
@Override
public void run() {
broadcastManager.registerReceiver(waitForBroadcastReceiver,
new IntentFilter(AppEventsLogger.ACTION_APP_EVENTS_FLUSHED));
}
}, true);
logger1.logEvent("an_event");
logger2.logEvent("another_event");
logger1.flush();
waitForBroadcastReceiver.waitForExpectedCalls();
closeBlockerAndAssertSuccess();
broadcastManager.unregisterReceiver(waitForBroadcastReceiver);
}
@Override
public void onPause() {
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.unregisterReceiver(mOnProfileInfoUploadListener);
manager.unregisterReceiver(mOnProfileInfoUpdateListener);
super.onPause();
}
@Test(timeout = 5000)
public void testOnLocationOnStoppedService() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(3);
BroadcastReceiver serviceBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
int action = bundle.getInt("action");
if (action == LocationServiceImpl.MSG_ON_SERVICE_STARTED) {
latch.countDown();
mService.stop();
//mService.onDestroy();
}
if (action == LocationServiceImpl.MSG_ON_SERVICE_STOPPED) {
latch.countDown();
mService.onLocation(new BackgroundLocation());
}
if (action == LocationServiceImpl.MSG_ON_LOCATION) {
latch.countDown();
}
}
};
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(InstrumentationRegistry.getTargetContext());
lbm.registerReceiver(serviceBroadcastReceiver, new IntentFilter(LocationServiceImpl.ACTION_BROADCAST));
Config config = Config.getDefault();
config.setStartForeground(false);
MockLocationProvider provider = new MockLocationProvider();
Location location = new Location("gps");
location.setProvider("mock");
location.setElapsedRealtimeNanos(2000000000L);
location.setAltitude(100);
location.setLatitude(49);
location.setLongitude(5);
location.setAccuracy(105);
location.setSpeed(50);
location.setBearing(1);
provider.setMockLocations(Arrays.asList(location));
mLocationProviderFactory.setProvider(provider);
mService.setLocationProviderFactory(mLocationProviderFactory);
mService.configure(config);
mService.start();
latch.await();
lbm.unregisterReceiver(serviceBroadcastReceiver);
}
private void close() {
LocalBroadcastManager broadcastManager =
LocalBroadcastManager.getInstance(applicationContext);
broadcastManager.unregisterReceiver(this);
}
private void unregisterLocalBroadcastManager() {
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mApplicationContext);
manager.unregisterReceiver(mBroadcastReceiver);
}
@Override
protected void onDestroy() {
handler.removeCallbacks(tryToConnectAgain);
MBApp application = MBApp.getApp();
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(application);
localBroadcastManager.unregisterReceiver(gattForceClosedReceiver);
localBroadcastManager.unregisterReceiver(connectionChangedReceiver);
if(dfuResultReceiver != null) {
localBroadcastManager.unregisterReceiver(dfuResultReceiver);
}
application.stopService(new Intent(application, DfuService.class));
super.onDestroy();
releaseViews();
}
private void close() {
LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(applicationContext);
broadcastManager.unregisterReceiver(this);
}
private void unregisterListeners() {
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
localBroadcastManager.unregisterReceiver(highlightReceiver);
localBroadcastManager.unregisterReceiver(readPositionReceiver);
localBroadcastManager.unregisterReceiver(closedReceiver);
}
public void testPersistedEvents() throws IOException, ClassNotFoundException {
AppEventsLogger.setFlushBehavior(AppEventsLogger.FlushBehavior.EXPLICIT_ONLY);
final WaitForBroadcastReceiver waitForBroadcastReceiver = new WaitForBroadcastReceiver();
final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
// Need to get notifications on another thread so we can wait for them.
runOnBlockerThread(new Runnable() {
@Override
public void run() {
broadcastManager.registerReceiver(waitForBroadcastReceiver,
new IntentFilter(AppEventsLogger.ACTION_APP_EVENTS_FLUSHED));
}
}, true);
getActivity().getFileStreamPath(AppEventsLogger.PersistedEvents.PERSISTED_EVENTS_FILENAME).delete();
TestSession session1 = TestSession.createSessionWithSharedUser(getActivity(), null);
AppEventsLogger logger1 = AppEventsLogger.newLogger(getActivity(), session1);
logger1.logEvent("an_event");
AppEventsLogger.onContextStop();
FileInputStream fis = getActivity().openFileInput(AppEventsLogger.PersistedEvents.PERSISTED_EVENTS_FILENAME);
assertNotNull(fis);
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
ois.close();
assertTrue(obj instanceof HashMap);
logger1.flush();
logger1.logEvent("another_event");
waitForBroadcastReceiver.incrementExpectCount();
logger1.flush();
waitForBroadcastReceiver.waitForExpectedCalls();
List<Intent> receivedIntents = waitForBroadcastReceiver.getReceivedIntents();
assertEquals(1, receivedIntents.size());
Intent intent = receivedIntents.get(0);
assertNotNull(intent);
assertEquals(2, intent.getIntExtra(AppEventsLogger.APP_EVENTS_EXTRA_NUM_EVENTS_FLUSHED, 0));
broadcastManager.unregisterReceiver(waitForBroadcastReceiver);
}
private void close() {
LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(applicationContext);
broadcastManager.unregisterReceiver(this);
}
@SmallTest
@MediumTest
@LargeTest
public void testSetActiveSession() {
Session.setActiveSession(null);
final WaitForBroadcastReceiver receiverOpened = new WaitForBroadcastReceiver();
final WaitForBroadcastReceiver receiverClosed = new WaitForBroadcastReceiver();
final WaitForBroadcastReceiver receiverSet = new WaitForBroadcastReceiver();
final WaitForBroadcastReceiver receiverUnset = new WaitForBroadcastReceiver();
final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
try {
Runnable initializeOnBlockerThread = new Runnable() {
@Override
public void run() {
broadcastManager.registerReceiver(receiverOpened,
getActiveSessionFilter(Session.ACTION_ACTIVE_SESSION_OPENED));
broadcastManager.registerReceiver(receiverClosed,
getActiveSessionFilter(Session.ACTION_ACTIVE_SESSION_CLOSED));
broadcastManager.registerReceiver(receiverSet,
getActiveSessionFilter(Session.ACTION_ACTIVE_SESSION_SET));
broadcastManager.registerReceiver(receiverUnset,
getActiveSessionFilter(Session.ACTION_ACTIVE_SESSION_UNSET));
}
};
runOnBlockerThread(initializeOnBlockerThread, true);
// null -> null should not fire events
assertEquals(null, Session.getActiveSession());
Session.setActiveSession(null);
assertEquals(null, Session.getActiveSession());
Session session0 = new Session.Builder(getActivity()).
setApplicationId("FakeAppId").
setTokenCachingStrategy(new MockTokenCachingStrategy()).
build();
assertEquals(SessionState.CREATED_TOKEN_LOADED, session0.getState());
// For unopened session, we should only see the Set event.
receiverSet.incrementExpectCount();
Session.setActiveSession(session0);
assertEquals(session0, Session.getActiveSession());
receiverSet.waitForExpectedCalls();
// When we open it, then we should see the Opened event.
receiverOpened.incrementExpectCount();
session0.openForRead(null);
receiverOpened.waitForExpectedCalls();
// Setting to itself should not fire events
Session.setActiveSession(session0);
assertEquals(session0, Session.getActiveSession());
// Setting from one opened session to another should deliver a full
// cycle of events
WaitForBroadcastReceiver.incrementExpectCounts(receiverClosed, receiverUnset, receiverSet, receiverOpened);
Session session1 = new Session.Builder(getActivity()).
setApplicationId("FakeAppId").
setTokenCachingStrategy(new MockTokenCachingStrategy()).
build();
assertEquals(SessionState.CREATED_TOKEN_LOADED, session1.getState());
session1.openForRead(null);
assertEquals(SessionState.OPENED, session1.getState());
Session.setActiveSession(session1);
WaitForBroadcastReceiver.waitForExpectedCalls(receiverClosed, receiverUnset, receiverSet, receiverOpened);
assertEquals(SessionState.CLOSED, session0.getState());
assertEquals(session1, Session.getActiveSession());
closeBlockerAndAssertSuccess();
} finally {
broadcastManager.unregisterReceiver(receiverOpened);
broadcastManager.unregisterReceiver(receiverClosed);
broadcastManager.unregisterReceiver(receiverSet);
broadcastManager.unregisterReceiver(receiverUnset);
Session.setActiveSession(null);
}
}
public static void unregister(Context context) {
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
localBroadcastManager.unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}