Phalconapp开发

BroadcastReceiver

2016-06-14  本文已影响175人  jacky123

注册方式

1.动态注册

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
registerReceiver(new ScreenBroadcastReceiver(), filter);

另外,注册广播可以通过IntentFilter的 setPriority设置广播的优先级。
如:

IntentFilter intentFilter = new IntentFilter(ACTION);

intentFilter.setPriority(1000);
registerReceiver(firstReceiver, intentFilter);

2.静态注册

<receiver android:name=".ScreenBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.SCREEN_OFF" />
        <action android:name="android.intent.action.USER_PRESENT" />
    </intent-filter>
</receiver>

也可以通过 android:priority来设置广播的优先级

<receiver android:name=".receiver.FirstReceivcer">
    <intent-filter android:priority="1000">
        <action android:name="com.jack.app.myapplication.action.ORDER_BROADCAST"></action>
    </intent-filter>
</receiver>

使用

public class ScreenBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        action = intent.getAction();
        if (Intent.ACTION_SCREEN_OFF.equals(action)) {              // 锁屏
            BaseActivity.ISSCREENOFF = true;
        } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
            BaseActivity.ISSCREENOFF = false;
        }
    }
}

无序广播与有序广播

有序广播接收,传值示例

注册略,请看上面的两者注册方式,FirstReceivcer 的优先级是1000,SecondReceiver的优先级是100.

public class FirstReceivcer extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int limit = intent.getIntExtra("limit",0);
        if (limit == 1000){
            String msg = intent.getStringExtra("msg");
            Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
            abortBroadcast();
        } else{
            Bundle b = new Bundle();
            b.putString("new","message from firstReceiver");
            setResultExtras(b);
        }
    }
}

public class SecondReceivcer extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int limit = intent.getIntExtra("limit", 0);
        if (limit == 100) {
            String msg = intent.getStringExtra("msg");

            /**
             * 取得上一个Receiver增加的信息
             */
            Bundle resultExtras = getResultExtras(true);
            String aNew = resultExtras.getString("new");

            Toast.makeText(context, "get message from first receiver:" + aNew, Toast.LENGTH_SHORT).show();
            abortBroadcast();
        } else {
            Bundle b = new Bundle();
            b.putString("new", "message from secondReceiver");
            setResultExtras(b);
        }
    }
}

在BroadcastReceiver的intent传递过程中

  1. 发送有序广播 sendOrderedBroadcast
Intent i = new Intent();
i.setAction(ACTION);
if (id == R.id.action_menu0){
    i.putExtra("limit", 100);
    i.putExtra("msg","有序广播");
    sendOrderedBroadcast(i, null);
}else if (id == R.id.action_menu1){
    i.putExtra("limit", 100);
    i.putExtra("msg","普通广播");
    sendBroadcast(i);
}
  1. 可以通过abortBroadcast取消广播的继续传递
  2. 在有序广播中,可以通过
Bundle b = new Bundle();
b.putString("new","message from firstReceiver");
setResultExtras(b);

来设置加入对广播传递信息的修改。可以通过getResultExtras取得上一个Receiver增加的信息。

/**
 * 取得上一个Receiver增加的信息
 */
Bundle resultExtras = getResultExtras(true);
String aNew = resultExtras.getString("new");

BroadcastReceiver 工作过程

静态注册的广播在应用安装的时候由系统自动完成注册,具体是由 PackageManagerService 来完成整个注册过程。这里只分析广播的动态注册的流程。

注册过程

  1. 通过ContextWrapper
@Override
public Intent registerReceiver(
    BroadcastReceiver receiver, IntentFilter filter) {
    return mBase.registerReceiver(receiver, filter);
}

mBase 是一个ContextImpl对象。

  1. ContextImpl的registerReceiver方法
    registerReceiver 又会去调用自己的 registerReceiverInternal ,ReceiverDispatcher是 LoadedApk的静态内部类。
private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
        IntentFilter filter, String broadcastPermission,
        Handler scheduler, Context context) {
    /**
     * IIntentReceiver 一个 Binder 对象,因为注册过程是一个跨进程通信的过程
     */
    IIntentReceiver rd = null;
    if (receiver != null) {
        if (mPackageInfo != null && context != null) {
            if (scheduler == null) {
                scheduler = mMainThread.getHandler();
            }
            rd = mPackageInfo.getReceiverDispatcher(
                receiver, context, scheduler,
                mMainThread.getInstrumentation(), true);
        } else {
            if (scheduler == null) {
                scheduler = mMainThread.getHandler();
            }
            rd = new LoadedApk.ReceiverDispatcher(
                    receiver, context, scheduler, null, true).getIIntentReceiver();
        }
    }
    try {
        return ActivityManagerNative.getDefault().registerReceiver(
                mMainThread.getApplicationThread(), mBasePackageName,
                rd, filter, broadcastPermission, userId);
    } catch (RemoteException e) {
        return null;
    }
}

ActivityManagerNative.getDefault()就是AMS。
这里关注下 ReceiverDispatcher 的 getReceiverDispatcher 实现,其先创建了 ReceiverDispatcher对象rd,再通过
rd.getIIntentReceiver();
返回 IIntentReceiver 对象,如下:

public IIntentReceiver getReceiverDispatcher(BroadcastReceiver r,
        Context context, Handler handler,
        Instrumentation instrumentation, boolean registered) {
    synchronized (mReceivers) {
        LoadedApk.ReceiverDispatcher rd = null;
        ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> map = null;
        if (registered) {
            map = mReceivers.get(context);
            if (map != null) {
                rd = map.get(r);
            }
        }
        if (rd == null) {
            rd = new ReceiverDispatcher(r, context, handler,
                    instrumentation, registered);
            if (registered) {
                if (map == null) {
                    map = new ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>();
                    mReceivers.put(context, map);
                }
                map.put(r, rd);
            }
        } else {
            rd.validate(context, handler);
        }
        rd.mForgotten = false;
        return rd.getIIntentReceiver();
    }
}
  1. 由于注册广播真正的过程是在AMS中,我们看看AMS的 registerReceiver。
public Intent registerReceiver(IApplicationThread caller, String callerPackage,
            IIntentReceiver receiver, IntentFilter filter, String permission, int userId) {
            ...
    rl = new ReceiverList(this, callerApp, callingPid, callingUid,
                userId, receiver);
    mRegisteredReceivers.put(receiver.asBinder(), rl);
    
    BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,
            permission, callingUid, userId);
    rl.add(bf);
    if (!bf.debugCheck()) {
        Slog.w(TAG, "==> For Dynamic broadcast");
    }
    mReceiverResolver.addFilter(bf);
    ...
            
}

最终会把远程的 InnerReceiver 对象以及 IntentFilter 对象存储起来,整个广播的注册就完成了。

广播的发送和接收过程

这里只分析普通广播的发送过程。广播的发送和接受,其本质是一个过程的两个阶段。我们从广播的发送说起:
Context 的 sendBroadcast 是一个抽象方法。和注册的过程一样,ContextWrapper 的sendBroadcast同样将事情交给了 ContextImpl来处理。

1. ContextImpl 的 sendBroadcast 过程如下:

@Override
public void sendBroadcast(Intent intent) {
    warnIfCallingFromSystemProcess();
    String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
    try {
        intent.prepareToLeaveProcess();
        ActivityManagerNative.getDefault().broadcastIntent(
                mMainThread.getApplicationThread(), intent, resolvedType, null,
                Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
                getUserId());
    } catch (RemoteException e) {
        throw new RuntimeException("Failure from system", e);
    }
}

ContextImpl 几乎什么事都没有干,直接向 AMS 发起了一个异步请求用于发送广播。

2. AMS 的 broadcastIntent 源码如下:

public final int broadcastIntent(IApplicationThread caller,
        Intent intent, String resolvedType, IIntentReceiver resultTo,
        int resultCode, String resultData, Bundle resultExtras,
        String[] requiredPermissions, int appOp, Bundle options,
        boolean serialized, boolean sticky, int userId) {
    enforceNotIsolatedCaller("broadcastIntent");
    synchronized(this) {
        intent = verifyBroadcastLocked(intent);

        final ProcessRecord callerApp = getRecordForAppLocked(caller);
        final int callingPid = Binder.getCallingPid();
        final int callingUid = Binder.getCallingUid();
        final long origId = Binder.clearCallingIdentity();
        int res = broadcastIntentLocked(callerApp,
                callerApp != null ? callerApp.info.packageName : null,
                intent, resolvedType, resultTo, resultCode, resultData, resultExtras,
                requiredPermissions, appOp, null, serialized, sticky,
                callingPid, callingUid, userId);
        Binder.restoreCallingIdentity(origId);
        return res;
    }
}

broadcastIntent 调用了 broadcastIntentLocked 。broadcastIntentLocked 代码比较多,看起来比较复杂。

// By default broadcasts do not go to stopped apps.
intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);

从 Android3.1开始广播就具有这种特性-默认情况下广播不会发送给已经停止的应用,这样做是为了防止广播无意间或者在不必要的时候调起已经停止运行的应用。在Android3.1中为Intent添加了两个标记位,分别为

1.FLAG_EXCLUDE_STOPPED_PACKAGES
表示不包含已经停止的应用,这个时候广播不会发送给已经停止的应用
2.FLAG_INCLUDE_STOPPED_PACKAGES
表示包含已经停止的应用,这个时候广播会发送给已经停止的应用

如果确实需要调起未启动的应用,那么只需要为广播的 Intent 添加 FLAG_INCLUDE_STOPPED_PACKAGES 标记即可。当 FLAG_EXCLUDE_STOPPED_PACKAGES 和 FLAG_INCLUDE_STOPPED_PACKAGES 两种标记并存时,以 FLAG_INCLUDE_STOPPED_PACKAGES 为准。

在 broadcastIntentLocked 内部,会根据 intent-filter 查找出匹配的广播接受者并经过一系列条件过滤。最终会把满足条件的广播接受者添加到 BroadcastQueue 中。

int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
if (!ordered && NR > 0) {
    // If we are not serializing this broadcast, then send the
    // registered receivers separately so they don't wait for the
    // components to be launched.
    final BroadcastQueue queue = broadcastQueueForIntent(intent);
    BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
            callerPackage, callingPid, callingUid, resolvedType, requiredPermission,
            appOp, registeredReceivers, resultTo, resultCode, resultData, map,
            ordered, sticky, false, userId);
    if (DEBUG_BROADCAST) Slog.v(
            TAG, "Enqueueing parallel broadcast " + r);
    final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r);
    if (!replaced) {
        queue.enqueueParallelBroadcastLocked(r);
        queue.scheduleBroadcastsLocked();
    }
    registeredReceivers = null;
    NR = 0;
}

3. 下面看下 BroadcastQueue 中广播的发送过程的实现

public void scheduleBroadcastsLocked() {
    if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts ["
            + mQueueName + "]: current="
            + mBroadcastsScheduled);

    if (mBroadcastsScheduled) {
        return;
    }
    mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
    mBroadcastsScheduled = true;
}

scheduleBroadcastsLocked并没有立即执行发送广播,而是发送了一个BROADCAST_INTENT_MSG类型的消息,BroadcastQueue 收到消息后会调用 BroadcastQueue 的 processNextBroadcast 方法,processNextBroadcast 方法对普通广播的处理如下:

// First, deliver any non-serialized broadcasts right away.
while (mParallelBroadcasts.size() > 0) {
    r = mParallelBroadcasts.remove(0);
    r.dispatchTime = SystemClock.uptimeMillis();
    r.dispatchClockTime = System.currentTimeMillis();
    final int N = r.receivers.size();
    if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast ["
            + mQueueName + "] " + r);
    for (int i=0; i<N; i++) {
        Object target = r.receivers.get(i);
        if (DEBUG_BROADCAST)  Slog.v(TAG,
                "Delivering non-ordered on [" + mQueueName + "] to registered "
                + target + ": " + r);
        deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
    }
    addBroadcastToHistoryLocked(r);
    if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["
            + mQueueName + "] " + r);
}

无序广播存储在 mParallelBroadcasts 中,系统会遍历 mParallelBroadcasts 并将其中的广播发送给他们所有的接受者。具体通过 deliverToRegisteredReceiverLocked 实现,deliverToRegisteredReceiverLocked 负责讲一个广播发送给一个特定的接受者,它内部调用了 performReceiveLocked 来完成具体的发送过程。

 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
    new Intent(r.intent), r.resultCode, r.resultData,
    r.resultExtras, r.ordered, r.initialSticky, r.userId);

performReceiveLocked 实现如下

private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
        Intent intent, int resultCode, String data, Bundle extras,
        boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
    // Send the intent to the receiver asynchronously using one-way binder calls.
    if (app != null) {
        if (app.thread != null) {
            // If we have an app thread, do the call through that so it is
            // correctly ordered with other one-way calls.
            /**
             * app.thread 仍然指 ApplicatonThread
             */
            app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
                    data, extras, ordered, sticky, sendingUser, app.repProcState);
        } else {
            // Application has died. Receiver doesn't exist.
            throw new RemoteException("app.thread must not be null");
        }
    } else {
        receiver.performReceive(intent, resultCode, data, extras, ordered,
                sticky, sendingUser);
    }
}

4. 在 ApplicationThread 的 scheduleRegisteredReceiver 中,通过 InnerReceiver 来实现广播的接收。

public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
        int resultCode, String dataStr, Bundle extras, boolean ordered,
        boolean sticky, int sendingUser, int processState) throws RemoteException {
    updateProcessState(processState, false);
    receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
            sticky, sendingUser);
}

通过 InnerReceiver 的performReceive方法会调用 LoadedApk.ReceiverDispatcher 的performReceive。方法如下:

public void performReceive(Intent intent, int resultCode, String data,
            Bundle extras, boolean ordered, boolean sticky, int sendingUser) {
        if (ActivityThread.DEBUG_BROADCAST) {
            int seq = intent.getIntExtra("seq", -1);
            Slog.i(ActivityThread.TAG, "Enqueueing broadcast " + intent.getAction() + " seq=" + seq
                    + " to " + mReceiver);
        }
        Args args = new Args(intent, resultCode, data, extras, ordered,
                sticky, sendingUser);
        if (!mActivityThread.post(args)) {
            if (mRegistered && ordered) {
                IActivityManager mgr = ActivityManagerNative.getDefault();
                if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
                        "Finishing sync broadcast to " + mReceiver);
                args.sendFinished(mgr);
            }
        }
    }

}

在上面的代码中,会创建一个 Args 对象,并通过 mActivityThread 的post方法执行Args中的逻辑,而 Args实现了 Runnable 接口。mActivityThread 是一个Handler,它其实就是 ActivityThread 中的 mH,mH 的类型是 ActivityThread的内部类H.在Args的run方法中有如下代码:

final BroadcastReceiver receiver = mReceiver;
receiver.setPendingResult(this);
receiver.onReceive(mContext, intent);

很显然,这个时候 BroadcastReceiver 的 onReceiver 执行了。Ok!

上一篇下一篇

猜你喜欢

热点阅读