Java Code Examples for android.content.Context#unregisterReceiver()

The following examples show how to use android.content.Context#unregisterReceiver() . 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: TargetChosenReceiver.java    From react-native-share with MIT License 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
public static IntentSender getSharingSenderIntent(ReactContext reactContext) {
    synchronized (LOCK) {
        if (sTargetChosenReceiveAction == null) {
            sTargetChosenReceiveAction = reactContext.getPackageName() + "/" + TargetChosenReceiver.class.getName() + "_ACTION";
        }
        Context context = reactContext.getApplicationContext();
        if (sLastRegisteredReceiver != null) {
            context.unregisterReceiver(sLastRegisteredReceiver);
        }
        sLastRegisteredReceiver = new TargetChosenReceiver();
        context.registerReceiver(sLastRegisteredReceiver, new IntentFilter(sTargetChosenReceiveAction));
    }

    Intent intent = new Intent(sTargetChosenReceiveAction);
    intent.setPackage(reactContext.getPackageName());
    intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode());
    final PendingIntent callback = PendingIntent.getBroadcast(reactContext, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    return callback.getIntentSender();
}
 
Example 2
Source File: USBMonitor.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
	 * unregister BroadcastReceiver
	 * @throws IllegalStateException
	 */
	public synchronized void unregister() throws IllegalStateException {
		// 接続チェック用Runnableを削除
		mDeviceCounts = 0;
		if (!destroyed) {
			mAsyncHandler.removeCallbacks(mDeviceCheckRunnable);
		}
		if (mPermissionIntent != null) {
//			if (DEBUG) Log.i(TAG, "unregister:");
			final Context context = mWeakContext.get();
			try {
				if (context != null) {
					context.unregisterReceiver(mUsbReceiver);
				}
			} catch (final Exception e) {
				Log.w(TAG, e);
			}
			mPermissionIntent = null;
		}
	}
 
Example 3
Source File: VolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override
public void onDestroy() {
       Context context = getContext();
       try {
           if (null != mVolumeMediaReceiver)
               context.unregisterReceiver(mVolumeMediaReceiver);
       } catch (IllegalArgumentException iae) {
           LOGE("VolumePanel", "Could not unregister volume/ media receiver.", iae);
       }
       if (null != mAppTypeMonitor)
           mAppTypeMonitor.unregister(context);
       if (null != mPriorityModeObserver)
           mPriorityModeObserver.setListening(false);
       mPriorityModeObserver = null;
	mAudioManager = null;
       mAppTypeMonitor = null;
	mVolumeMediaReceiver = null;
       AudioHelper.freeResources();
       if (null != mAudioHelper)
           mAudioHelper.setHandler(null);
       if (null != mMediaProviderDelegate)
           mMediaProviderDelegate.destroy();
       mMediaProviderDelegate = null;
       mAudioHelper = null;
       try {
           if (registeredOtto) MainThreadBus.get().unregister(this);
       } catch (IllegalArgumentException e) {
           LOGE("VolumePanel", "Failed to unregister our VolumePanel from Otto.");
       }
	super.onDestroy();
}
 
Example 4
Source File: BaseReceiver.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
/**
 * 解注册本广播接收者
 * @param context Context
 * @param unRegisterInLocal 是否解注册本广播为本地广播
 */
public void unRegister(Context context, boolean unRegisterInLocal) {
    if (unRegisterInLocal) {
        LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
    }
    else{
        context.unregisterReceiver(this);
    }
}
 
Example 5
Source File: ShareHelper.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
static void sendChooserIntent(boolean saveLastUsed, Activity activity,
                              Intent sharingIntent,
                              @Nullable TargetChosenCallback callback) {
    synchronized (LOCK) {
        if (sTargetChosenReceiveAction == null) {
            sTargetChosenReceiveAction = activity.getPackageName() + "/"
                    + TargetChosenReceiver.class.getName() + "_ACTION";
        }
        Context context = activity.getApplicationContext();
        if (sLastRegisteredReceiver != null) {
            context.unregisterReceiver(sLastRegisteredReceiver);
            // Must cancel the callback (to satisfy guarantee that exactly one method of
            // TargetChosenCallback is called).
            // TODO(mgiuca): This should be called immediately upon cancelling the chooser,
            // not just when the next share takes place (https://crbug.com/636274).
            sLastRegisteredReceiver.cancel();
        }
        sLastRegisteredReceiver = new TargetChosenReceiver(saveLastUsed, callback);
        context.registerReceiver(
                sLastRegisteredReceiver, new IntentFilter(sTargetChosenReceiveAction));
    }

    Intent intent = new Intent(sTargetChosenReceiveAction);
    intent.setPackage(activity.getPackageName());
    intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode());
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    Intent chooserIntent = Intent.createChooser(sharingIntent,
            activity.getString(R.string.share_link_chooser_title),
            pendingIntent.getIntentSender());
    if (sFakeIntentReceiverForTesting != null) {
        sFakeIntentReceiverForTesting.setIntentToSendBack(intent);
    }
    fireIntent(activity, chooserIntent);
}
 
Example 6
Source File: HttpNegotiateAuthenticator.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void run(AccountManagerFuture<Bundle> future) {
    Bundle result;
    try {
        result = future.getResult();
    } catch (OperationCanceledException | AuthenticatorException | IOException e) {
        Log.w(TAG, "ERR_UNEXPECTED: Error while attempting to obtain a token.", e);
        nativeSetResult(mRequestData.nativeResultObject, NetError.ERR_UNEXPECTED, null);
        return;
    }

    if (result.containsKey(AccountManager.KEY_INTENT)) {
        final Context appContext = ContextUtils.getApplicationContext();

        // We wait for a broadcast that should be sent once the user is done interacting
        // with the notification
        // TODO(dgn) We currently hang around if the notification is swiped away, until
        // a LOGIN_ACCOUNTS_CHANGED_ACTION filter is received. It might be for something
        // unrelated then we would wait again here. Maybe we should limit the number of
        // retries in some way?
        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                appContext.unregisterReceiver(this);
                mRequestData.accountManager.getAuthToken(mRequestData.account,
                        mRequestData.authTokenType, mRequestData.options,
                        true /* notifyAuthFailure */, new GetTokenCallback(mRequestData),
                        null);
            }

        };
        appContext.registerReceiver(broadcastReceiver,
                new IntentFilter(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION));
    } else {
        processResult(result, mRequestData);
    }
}
 
Example 7
Source File: ShareHelper.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
static void sendChooserIntent(boolean saveLastUsed, Activity activity,
                              Intent sharingIntent,
                              @Nullable TargetChosenCallback callback) {
    synchronized (LOCK) {
        if (sTargetChosenReceiveAction == null) {
            sTargetChosenReceiveAction = activity.getPackageName() + "/"
                    + TargetChosenReceiver.class.getName() + "_ACTION";
        }
        Context context = activity.getApplicationContext();
        if (sLastRegisteredReceiver != null) {
            context.unregisterReceiver(sLastRegisteredReceiver);
            // Must cancel the callback (to satisfy guarantee that exactly one method of
            // TargetChosenCallback is called).
            // TODO(mgiuca): This should be called immediately upon cancelling the chooser,
            // not just when the next share takes place (https://crbug.com/636274).
            sLastRegisteredReceiver.cancel();
        }
        sLastRegisteredReceiver = new TargetChosenReceiver(saveLastUsed, callback);
        context.registerReceiver(
                sLastRegisteredReceiver, new IntentFilter(sTargetChosenReceiveAction));
    }

    Intent intent = new Intent(sTargetChosenReceiveAction);
    intent.setPackage(activity.getPackageName());
    intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode());
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    Intent chooserIntent = Intent.createChooser(sharingIntent,
            activity.getString(R.string.share_link_chooser_title),
            pendingIntent.getIntentSender());
    if (sFakeIntentReceiverForTesting != null) {
        sFakeIntentReceiverForTesting.setIntentToSendBack(intent);
    }
    fireIntent(activity, chooserIntent);
}
 
Example 8
Source File: WifiSkillViewFragment.java    From Easer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onDetach() {
    //noinspection ConstantConditions
    @NonNull Context context = getContext();
    wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    context.unregisterReceiver(mReceiver);
    super.onDetach();
}
 
Example 9
Source File: U2FTransportAndroid.java    From android-u2f-bridge with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
   String deviceName = usbDevice.getDeviceName();

   if (ACTION_USB_PERMISSION.equals(action)) {
      boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,
            false);
      // sync with connect
      gotRights.clear();
      gotRights.add(permission);
      context.unregisterReceiver(mUsbReceiver);
   }
}
 
Example 10
Source File: NetworkController.java    From DebugDrawer with Apache License 2.0 5 votes vote down vote up
/**
 * Unregister network state broadcast receiver
 */
void unregisterReceiver() {
    try {
        final Context context = contextRef.get();
        if (context != null) {
            context.unregisterReceiver(receiver);
        }
    } catch (IllegalArgumentException e) {
    }
}
 
Example 11
Source File: BluetoothStateReceiver.java    From SimpleBluetoothLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * Safe method to unregister the receiver in case of errors. Still unregisters the receiver for
 * all filters it has been registered for.
 * @param context the context that had registered the receiver
 * @param receiver the receiver that was registered.
 */
public static void safeUnregister(Context context, BluetoothStateReceiver receiver) {
    try {
        context.unregisterReceiver(receiver);
    } catch(IllegalStateException e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: PhoneReceiver.java    From android-common with Apache License 2.0 5 votes vote down vote up
public void unRegisterReceiver(Context context) {
    try {
        context.unregisterReceiver(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 13
Source File: ScreenLockReceiver.java    From zone-sdk with MIT License 5 votes vote down vote up
public void unRegisterScreenReceiver(Context context) {
    try {
        context.unregisterReceiver(this);
        LogZSDK.INSTANCE.d("注销屏幕解锁、加锁广播接收者...");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: AudioUtils.java    From Conquer with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent != null && Constants.ACTION_DESTORY_PLAYER.equals(intent.getAction())) {
        stopAnim();
        releasePlayer();
        L.d("停止音乐");
        context.unregisterReceiver(this);
        receiver = null;
    }
}
 
Example 15
Source File: NetworkConnectionHelper.java    From zapp with MIT License 5 votes vote down vote up
public void endListenForNetworkChanges() {
	Context context = contextReference.get();
	if (context == null) {
		return;
	}

	this.listener = null;
	context.unregisterReceiver(networkReceiver);
}
 
Example 16
Source File: AppVerUpdater.java    From AppVerUpdater with Apache License 2.0 5 votes vote down vote up
public void onStop(Context context) {
    if (networkReceiver != null) {
        try {
            context.unregisterReceiver(networkReceiver);
            networkReceiver = null;
        } catch (IllegalArgumentException e) {
            if (BuildConfig.DEBUG) {
                Log.e(TAG, "Exception: ", e);
            }
        }
    }
}
 
Example 17
Source File: MSSService.java    From MainScreenShow with GNU General Public License v2.0 4 votes vote down vote up
public void unRegisterScreenActionReceiver(Context mContext) {
    if (isRegisterReceiver) {
        isRegisterReceiver = false;
        mContext.unregisterReceiver(ScreenReceiver.this);
    }
}
 
Example 18
Source File: BroadcastManager.java    From Musicoco with Apache License 2.0 4 votes vote down vote up
/**
 * 注销广播接收者
 */
public void unregisterReceiver(Context context, BroadcastReceiver receiver) {
    context.unregisterReceiver(receiver);
}
 
Example 19
Source File: AccountChangeReceiver.java    From letv with Apache License 2.0 4 votes vote down vote up
public void unregisterLogon(Context context) {
    context.unregisterReceiver(this);
}
 
Example 20
Source File: HeadsetPlugReceiver.java    From Augendiagnose with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Unregister this receiver.
 *
 * @param context The context
 */
public void unregister(final Context context) {
	context.unregisterReceiver(this);
}