Java Code Examples for android.support.v4.content.LocalBroadcastManager#registerReceiver()

The following examples show how to use android.support.v4.content.LocalBroadcastManager#registerReceiver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocationServiceProxyTest.java    From background-geolocation-android with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: DownloadDialogActivity.java    From ESeal with Apache License 2.0 6 votes vote down vote up
/**
 * 注册广播
 */
private void broadcast() {
    LocalBroadcastManager broadcastManager = LocalBroadcastManager
            .getInstance(this);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter
            .addAction("com.dou361.update.downloadBroadcast");
    /** 建议把它写一个公共的变量,这里方便阅读就不写了 */
    BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            /** 刷新数据 */
            long type = intent.getLongExtra("type", 0);
            updateProgress(type);
        }
    };
    broadcastManager.registerReceiver(mItemViewListClickReceiver,
            intentFilter);
}
 
Example 3
Source File: BulkReadCardsDialogFragment.java    From Walrus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onResume() {
    super.onResume();

    getActivity().bindService(new Intent(getActivity(), BulkReadCardsService.class),
            bulkReadCardsServiceConnection, 0);

    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
            getActivity());
    localBroadcastManager.registerReceiver(
            runnerUpdateBroadcastReceiver,
            new IntentFilter(BulkReadCardDataOperationRunner.ACTION_UPDATE));
    localBroadcastManager.registerReceiver(
            serviceUpdateBroadcastReceiver,
            new IntentFilter(BulkReadCardsService.ACTION_UPDATE));
}
 
Example 4
Source File: DfuBaseService.java    From microbit with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    initialize();

    final LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
    final IntentFilter actionFilter = makeDfuActionIntentFilter();
    manager.registerReceiver(mDfuActionReceiver, actionFilter);
    registerReceiver(mDfuActionReceiver, actionFilter); // Additionally we must register this receiver as a non-local to get broadcasts from the notification actions

    final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(mConnectionStateBroadcastReceiver, filter);

    final IntentFilter bondFilter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    registerReceiver(mBondStateBroadcastReceiver, bondFilter);
}
 
Example 5
Source File: MainActivity.java    From hprof-tools with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Initialize HprofCatcher so that any uncaught exception is intercepted
    HprofCatcher.init(this);

    setContentView(R.layout.activity_main);
    mStatus = (TextView) findViewById(R.id.status);
    mInput = (TextView) findViewById(R.id.input);
    mOutput = (TextView) findViewById(R.id.output);
    findViewById(R.id.fillMemory).setOnClickListener(this);
    findViewById(R.id.npe).setOnClickListener(this);

    // Register a receiver to listen for updates of the crunching status
    LocalBroadcastManager bm = LocalBroadcastManager.getInstance(this);
    IntentFilter filter = new IntentFilter();
    filter.addAction(CruncherService.ACTION_CRUNCHING_STARTED);
    filter.addAction(CruncherService.ACTION_CRUNCHING_FINISHED);
    bm.registerReceiver(mStatusReceiver, filter);
}
 
Example 6
Source File: LocationServiceProxyTest.java    From background-geolocation-android with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 7
Source File: OSelectionField.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
private void startSearchableActivity() {
    LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(mContext);
    Intent intent = new Intent(mContext, SearchableItemActivity.class);
    intent.putExtra("resource_id", mResourceArray);
    intent.putExtra("selected_position", getPos());
    intent.putExtra(OColumn.ROW_ID, getPos());
    intent.putExtra("search_hint", getLabel());
    if (mCol != null) {
        intent.putExtra("column_name", mCol.getName());
        if (mCol.hasDomainFilterColumn()) {
            OValues formValues = formView.getControlValues();
            Bundle formData = formValues != null ?
                    formValues.toFilterColumnsBundle(mModel, mCol) : new Bundle();
            intent.putExtra("form_data", formData);
        }
    }
    intent.putExtra("model", mModel.getModelName());
    intent.putExtra("live_search", (mWidget == OField.WidgetType.SearchableLive));
    broadcastManager.registerReceiver(valueReceiver, new IntentFilter("searchable_value_select"));
    mContext.startActivity(intent);
}
 
Example 8
Source File: FirmwareUpdater.java    From Bluefruit_LE_Connect_Android with MIT License 5 votes vote down vote up
private void registerAsDfuListener(boolean register) {
    final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(mContext);
    if (register) {
        broadcastManager.registerReceiver(mDfuUpdateReceiver, makeDfuUpdateIntentFilter());
    } else {
        broadcastManager.unregisterReceiver(mDfuUpdateReceiver);
    }
}
 
Example 9
Source File: MainActivity.java    From journaldev with MIT License 5 votes vote down vote up
private void registerReceiver() {

        LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PROGRESS_UPDATE);
        bManager.registerReceiver(mBroadcastReceiver, intentFilter);

    }
 
Example 10
Source File: DeviceOperationActivity.java    From ESeal with Apache License 2.0 5 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
    IntentFilter filter = new IntentFilter();
    filter.addAction(TakePictueActivity.PICTURE_URI);

    localBroadcastManager.registerReceiver(broadcastReceiver, filter);

    mNfcUtility = new NfcUtility(tagCallback);
}
 
Example 11
Source File: XmppConnectReceiver.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
private XmppConnectReceiver(Context context, XmppManager xmppManager) {
    this.context = context;
    this.xmppManager = xmppManager;
    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
    IntentFilter filter = new IntentFilter();
    filter.addAction(BroadcastUtil.APN_ACTION_LOGIN);
    filter.addAction(BroadcastUtil.APN_ACTION_RECONNECT);
    filter.addAction(BroadcastUtil.APN_ACTION_REQUEST_STATUS);
    filter.addAction(BroadcastUtil.APN_ACTION_RECEIPT);
    lbm.registerReceiver(this, filter);

    BroadcastUtil.sendBroadcast(context, BroadcastUtil.ANDROIDPN_MSG_RECEIVER_READY);

    sharedPrefs = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
            Context.MODE_PRIVATE);

    xmppHost = sharedPrefs.getString(Constants.XMPP_HOST, "localhost");
    xmppPort = sharedPrefs.getInt(Constants.XMPP_PORT, 5222);


    HandlerThread thread = new HandlerThread(XmppConnectReceiver.class.getSimpleName());
    thread.start();
    handler = new Handler(thread.getLooper());
    disconnectTask = new DisconnectTask();
    reconnectTask = new ReconnectTask();

    loginServerTask = new LoginServer();

}
 
Example 12
Source File: LoginActivity.java    From AvI with MIT License 5 votes vote down vote up
private void instantiateRequiredObjectsForLogin(){
    LockContext.configureLock(
            new Lock.Builder()
                    .loadFromApplication(getApplication())
                    .closable(true));

    final LocalBroadcastManager broadcastManager =
            LocalBroadcastManager.getInstance(getApplicationContext());
    broadcastManager.registerReceiver(receiver, new IntentFilter(Lock.AUTHENTICATION_ACTION));
}
 
Example 13
Source File: FolioReader.java    From ankihelper with GNU General Public License v3.0 5 votes vote down vote up
private FolioReader(Context context) {
    this.context = context;
    DbAdapter.initialize(context);

    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
    localBroadcastManager.registerReceiver(highlightReceiver,
            new IntentFilter(HighlightImpl.BROADCAST_EVENT));
    localBroadcastManager.registerReceiver(readPositionReceiver,
            new IntentFilter(ACTION_SAVE_READ_POSITION));
    localBroadcastManager.registerReceiver(closedReceiver,
            new IntentFilter(ACTION_FOLIOREADER_CLOSED));
}
 
Example 14
Source File: LikeView.java    From kognitivo with Apache License 2.0 5 votes vote down vote up
private void associateWithLikeActionController(LikeActionController likeActionController) {
    this.likeActionController = likeActionController;

    this.broadcastReceiver = new LikeControllerBroadcastReceiver();
    LocalBroadcastManager localBroadcastManager =
            LocalBroadcastManager.getInstance(getContext());

    // add the broadcast receiver
    IntentFilter filter = new IntentFilter();
    filter.addAction(LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_UPDATED);
    filter.addAction(LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_DID_ERROR);
    filter.addAction(LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_DID_RESET);

    localBroadcastManager.registerReceiver(broadcastReceiver, filter);
}
 
Example 15
Source File: AppListItemController.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
public void bindModel(@NonNull App app) {
    currentApp = app;

    if (actionButton != null) actionButton.setEnabled(true);

    Utils.setIconFromRepoOrPM(app, icon, activity);

    // Figures out the current install/update/download/etc status for the app we are viewing.
    // Then, asks the view to update itself to reflect this status.
    Iterator<AppUpdateStatus> statuses =
            AppUpdateStatusManager.getInstance(activity).getByPackageName(app.packageName).iterator();
    if (statuses.hasNext()) {
        AppUpdateStatus status = statuses.next();
        updateAppStatus(app, status);
    } else {
        updateAppStatus(app, null);
    }

    final LocalBroadcastManager broadcastManager =
            LocalBroadcastManager.getInstance(activity.getApplicationContext());
    broadcastManager.unregisterReceiver(onStatusChanged);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(AppUpdateStatusManager.BROADCAST_APPSTATUS_ADDED);
    intentFilter.addAction(AppUpdateStatusManager.BROADCAST_APPSTATUS_REMOVED);
    intentFilter.addAction(AppUpdateStatusManager.BROADCAST_APPSTATUS_CHANGED);
    broadcastManager.registerReceiver(onStatusChanged, intentFilter);
}
 
Example 16
Source File: TwilioVoicePlugin.java    From cordova-plugin-twiliovoicesdk with MIT License 4 votes vote down vote up
/**
 * Android Cordova Action Router
 * <p>
 * Executes the request.
 * <p>
 * This method is called from the WebView thread. To do a non-trivial amount
 * of work, use: cordova.getThreadPool().execute(runnable);
 * <p>
 * To run on the UI thread, use:
 * cordova.getActivity().runOnUiThread(runnable);
 *
 * @param action          The action to execute.
 * @param args            The exec() arguments in JSON form.
 * @param callbackContext The callback context used when calling back into JavaScript.
 * @return Whether the action was valid.
 */
@Override
public boolean execute(final String action, final JSONArray args,
                       final CallbackContext callbackContext) throws JSONException {
    if ("initializeWithAccessToken".equals(action)) {
        Log.d(TAG, "Initializing with Access Token");

        mAccessToken = args.optString(0);

        mInitCallbackContext = callbackContext;

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_SET_FCM_TOKEN);
        intentFilter.addAction(ACTION_INCOMING_CALL);
        LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(cordova.getActivity());
        lbm.registerReceiver(mBroadcastReceiver, intentFilter);

        if (mIncomingCallIntent != null) {
            Log.d(TAG, "initialize(): Handle an incoming call");
            handleIncomingCallIntent(mIncomingCallIntent);
            mIncomingCallIntent = null;
        }

        javascriptCallback("onclientinitialized", mInitCallbackContext);

        return true;

    } else if ("call".equals(action)) {
        call(args, callbackContext);
        return true;
    } else if ("acceptCallInvite".equals(action)) {
        acceptCallInvite(args, callbackContext);
        return true;
    } else if ("disconnect".equals(action)) {
        disconnect(args, callbackContext);
        return true;
    } else if ("sendDigits".equals(action)) {
        sendDigits(args, callbackContext);
        return true;
    } else if ("muteCall".equals(action)) {
        muteCall(callbackContext);
        return true;
    } else if ("unmuteCall".equals(action)) {
        unmuteCall(callbackContext);
        return true;
    } else if ("isCallMuted".equals(action)) {
        isCallMuted(callbackContext);
        return true;
    } else if ("callStatus".equals(action)) {
        callStatus(callbackContext);
        return true;
    } else if ("rejectCallInvite".equals(action)) {
        rejectCallInvite(args, callbackContext);
        return true;
    } else if ("showNotification".equals(action)) {
        showNotification(args, callbackContext);
        return true;
    } else if ("cancelNotification".equals(action)) {
        cancelNotification(args, callbackContext);
        return true;
    } else if ("setSpeaker".equals(action)) {
        setSpeaker(args, callbackContext);
        return true;
    }

    return false;
}
 
Example 17
Source File: Discoverer.java    From DronesWear with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void registerReceivers() {
    LocalBroadcastManager localBroadcastMgr = LocalBroadcastManager.getInstance(mCtx);
    localBroadcastMgr.registerReceiver(mArdiscoveryServicesDevicesListUpdatedReceiver, new IntentFilter(ARDiscoveryService.kARDiscoveryServiceNotificationServicesDevicesListUpdated));
}
 
Example 18
Source File: LocationServiceTest.java    From background-geolocation-android with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 19
Source File: SKActivity.java    From SensingKit-Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void registerLocalBroadcastManager() {
    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mApplicationContext);
    manager.registerReceiver(mBroadcastReceiver, new IntentFilter(SKActivityRecognitionIntentService.BROADCAST_UPDATE));
}
 
Example 20
Source File: BarometerNetworkActivity.java    From PressureNet with GNU General Public License v3.0 4 votes vote down vote up
private void registerForDownloadResults() {
	LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
	IntentFilter intentFilter = new IntentFilter();
	intentFilter.addAction(DATA_DOWNLOAD_RESULTS);
	bManager.registerReceiver(bReceiver, intentFilter);
}