下面列出了android.content.Context#unregisterReceiver ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
@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();
}
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);
}
}
}
}
public void endListenForNetworkChanges() {
Context context = contextReference.get();
if (context == null) {
return;
}
this.listener = null;
context.unregisterReceiver(networkReceiver);
}
@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;
}
}
public void unRegisterScreenReceiver(Context context) {
try {
context.unregisterReceiver(this);
LogZSDK.INSTANCE.d("注销屏幕解锁、加锁广播接收者...");
} catch (Exception e) {
e.printStackTrace();
}
}
public void unRegisterReceiver(Context context) {
try {
context.unregisterReceiver(this);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 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();
}
}
@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();
}
/**
* Unregister network state broadcast receiver
*/
void unregisterReceiver() {
try {
final Context context = contextRef.get();
if (context != null) {
context.unregisterReceiver(receiver);
}
} catch (IllegalArgumentException e) {
}
}
@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);
}
}
@Override
public void onDetach() {
//noinspection ConstantConditions
@NonNull Context context = getContext();
wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
context.unregisterReceiver(mReceiver);
super.onDetach();
}
@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);
}
@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);
}
}
@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);
}
/**
* 解注册本广播接收者
* @param context Context
* @param unRegisterInLocal 是否解注册本广播为本地广播
*/
public void unRegister(Context context, boolean unRegisterInLocal) {
if (unRegisterInLocal) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
}
else{
context.unregisterReceiver(this);
}
}
public void unregisterLogon(Context context) {
context.unregisterReceiver(this);
}
public void unRegisterScreenActionReceiver(Context mContext) {
if (isRegisterReceiver) {
isRegisterReceiver = false;
mContext.unregisterReceiver(ScreenReceiver.this);
}
}
/**
* 注销广播接收者
*/
public void unregisterReceiver(Context context, BroadcastReceiver receiver) {
context.unregisterReceiver(receiver);
}
/**
* Unregister this receiver.
*
* @param context The context
*/
public void unregister(final Context context) {
context.unregisterReceiver(this);
}