If u registerReceiver twice what will happen?

If u do registerReceiver two times  then it will be added twice in the arraylist as follows
/frameworks/base/services/tests/servicestests/src/com/android/server/BroadcastInterceptingContext.java
@Override
    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        synchronized (mInterceptors) {
            mInterceptors.add(new BroadcastInterceptor(receiver, filter));
        }
        return null;
    }
@Override
    public void sendBroadcast(Intent intent) {
        synchronized (mInterceptors) {
            final Iterator<BroadcastInterceptor> i = mInterceptors.iterator();
            while (i.hasNext()) {
                final BroadcastInterceptor interceptor = i.next();
                if (interceptor.dispatchBroadcast(intent)) {
                    i.remove();
                }
            }
        }
    }
public boolean dispatchBroadcast(Intent intent) {
            if (mFilter.match(getContentResolver(), intent, false, TAG) > 0) {
                if (mReceiver != null) {
                    final Context context = BroadcastInterceptingContext.this;
                    mReceiver.onReceive(context, intent);
                    return false;
                } else {
                    set(intent);
                    return true;
                }
            } else {
                return false;
            }
        }
So if it is added two times in a arraylist then clearly above method will invoke onReceive two times.

Similarly if u call startservice two times it will also call OnStatCommand twice see below comments from developer's site
Multiple requests to start the service result in multiple corresponding calls to the service's onStartCommand(). However, only one request to stop the service (with stopSelf() or stopService()) is required to stop it.

Comments

Popular posts from this blog

Wrong java version. 1.8 or 9 is required

How to make SystemService in Android