当前位置: 首页 > news >正文

网站开发应该怎么做在线网站流量查询

网站开发应该怎么做,在线网站流量查询,网站建设运营合同模板,如何做网站模特前言 前面已经讲了什么是context以及从源码角度分析context创建流程(上)。限于篇幅把四大组件中的广播和内容提供器的context获取流程放在了这篇文章。广播和内容提供器并不是context家族里的一员,所以他们本身并不是context,因而…

前言

前面已经讲了什么是context以及从源码角度分析context创建流程(上)。限于篇幅把四大组件中的广播和内容提供器的context获取流程放在了这篇文章。广播和内容提供器并不是context家族里的一员,所以他们本身并不是context,因而他们的context肯定是直接或间接从Application、Activity或者Service获取。然后对context的设计进行了讨论,从更高的角度看context,能够帮助我们看到context的本质,也能帮助我们更好地理解并使用context。

Broadcast的context获取流程

Broadcast和上面的组件不同,他并不是继承自Context,所以他的Context是需要通过Application、Activity或者Service来给予。我们一般使用广播的context是在接收器中,如:

class MyClass :BroadcastReceiver() {override fun onReceive(context: Context?, intent: Intent?) {TODO("use context")}
}

那么onReceive的context对象是从哪里来的呢?同样我们先看广播接收器的注册流程:

同样,详细的广播相关工作流程可以阅读Android广播Broadcast的注册与广播源码过程详解(基于api29)这篇文章了解。因为在创建Receiver的时候并没有传入context,所以我们需要追踪他的注册流程,看看在哪里获取了context。我们先看到ContextImpl的registerReceiver方法:

ContextImpl.class(api29)
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,String broadcastPermission, Handler scheduler) {// 注意参数return registerReceiverInternal(receiver, getUserId(),filter, broadcastPermission, scheduler, getOuterContext(), 0);
}

registerReceiver方法最终会来到这个重载方法,我们可以注意到,这里有个getOuterContext,这个是什么?还记得Activity的context创建过程吗?这个方法获取的就是activity本身。我们继续看下去:

ContextImpl.class(api29)
private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,IntentFilter filter, String broadcastPermission,Handler scheduler, Context context, int flags) {IIntentReceiver rd = null;if (receiver != null) {if (mPackageInfo != null && context != null) {...rd = mPackageInfo.getReceiverDispatcher(receiver, context, scheduler,mMainThread.getInstrumentation(), true);}...}...
}

这里利用context创建了ReceiverDispatcher,我们继续深入看:

LoadedApk.class(api29)
public IIntentReceiver getReceiverDispatcher(BroadcastReceiver r,Context context, Handler handler,Instrumentation instrumentation, boolean registered) {synchronized (mReceivers) {LoadedApk.ReceiverDispatcher rd = null;...if (rd == null) {rd = new ReceiverDispatcher(r, context, handler,instrumentation, registered);...}...}
}ReceiverDispatcher.class(api29)
ReceiverDispatcher(..., Context context,...) {...mContext = context;...
}

这里确实把receiver和context创建了ReceiverDispatcher,嗯?怎么没有给Receiver?其实这涉及到广播的内部设计结构。Receiver是没有跨进程通信能力的,而广播需要AMS的调控,所以必须有一个可以跟AMS沟通的对象,这个对象是InnerReceiver,而ReceiverDispatcher就是负责维护他们两个的联系,如下图:

而onReceive方法也是由ReceiverDispatcher回调的,最后我们再看到回调onReceive的那部分代码:

ReceiverDispatcher.java/Args.class;
public final Runnable getRunnable() {return () -> {...;try {...;// 可以看到这里回调了receiver的方法,这样整个接收广播的流程就走完了。receiver.onReceive(mContext, intent);}}
}

Args是Receiver的内部类,mContext就是在创建ReceiverDispatcher时传入的对象,到这里我们就知道这个对象确实是Activity了。

但是,,不一定每个都是Activity。在源码中我们知道是通过getOuterContext来获取context,如果是通过别的context注册广播,那么对应的对象也就不同了,只是我们一般都是在Activity中创建广播,所以这个context一般是activity对象。

ContentProvider的context获取流程

ContextProvider我们用的就比较少了,内容提供器主要是用于应用间内容共享的。虽然ContentProvider是由系统创建的,但是他本身并不属于Context家族体系内,所以他的context也是从其他获取的。老样子,先看ContentProvider的创建流程:

咦?这不是Application创建的流程图吗?是的,ContentProvider是伴随着应用启动被创建的,来看一张更加详细的流程图:

我们把目光聚集到ContentProvider的创建上,也就是installContentProviders方法。同样,详细的ContentProvider工作流程可以访问Android中ContentProvider的启动与请求源码流程详解(基于api29)这篇文章。installContentProviders是在handleBindApplication中被调用的,我们看到调用这个方法的地方:

private void handleBindApplication(AppBindData data) {try {// 创建Applicationapp = data.info.makeApplication(data.restrictedBackupMode, null);...if (!data.restrictedBackupMode) {if (!ArrayUtils.isEmpty(data.providers)) {// 安装ContentProviderinstallContentProviders(app, data.providers);}}    
}

可以看到这里传入了application对象,我们继续看下去:

private void installContentProviders(Context context, List<ProviderInfo> providers) {final ArrayList<ContentProviderHolder> results = new ArrayList<>();for (ProviderInfo cpi : providers) {...ContentProviderHolder cph = installProvider(context, null, cpi,false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);...}
...
}

这里调用了installProvider,继续往下看:

private ContentProviderHolder installProvider(Context context,ContentProviderHolder holder, ProviderInfo info,boolean noisy, boolean noReleaseNeeded, boolean stable) {ContentProvider localProvider = null;IContentProvider provider;if (holder == null || holder.provider == null) {...// 这里c最终是由context构造的Context c = null;ApplicationInfo ai = info.applicationInfo;if (context.getPackageName().equals(ai.packageName)) {c = context;}...try {// 创建ContentProviderfinal java.lang.ClassLoader cl = c.getClassLoader();LoadedApk packageInfo = peekPackageInfo(ai.packageName, true);...localProvider = packageInfo.getAppFactory().instantiateProvider(cl, info.name);provider = localProvider.getIContentProvider();...// 把context设置给ContentProviderlocalProvider.attachInfo(c, info);} ...} ...
}

这里最重要的一行代码是localProvider.attachInfo(c, info);,在这里把context设置给了ContentProvider,我们再深入一点看看:

ContentProvider.class(api29)
public void attachInfo(Context context, ProviderInfo info) {attachInfo(context, info, false);
}
private void attachInfo(Context context, ProviderInfo info, boolean testing) {...if (mContext == null) {mContext = context;...}...
}

这里确实把context赋值给了ContentProvider的内部变量mContext,这样ContentProvider就可以使用Context了。而这个context正是一开始传进来的Application。

总结

Context承受的两大重要职责是:身份权限、程序访问系统的接口。一个Java类,如果没有context那么就是一个普通的Java类,而当他获得context那么他就可以称之为一个组件了,因为它获得了访问系统的权限,他不再是一个普通的身份,是属于android“公民”了。而“公民”并不是无法无天,系统也可以通过context来封装以及限制程序的权限。要想弹出一个通知,你必须通过这个api,用户关闭你的通知权限,你就别想通过第二条路来弹出通知了。同时 程序也无需知道底层到底是如何实现,只管调用api即可。四大组件为何称为四大组件,因为他们生来就有了context,特别是activity和service,包括Application。而我们写的一切程序,都必须间接或者直接从其中获取context。

总而言之,context就是负责区分android内外程序的一个机制,限制程序访问系统资源的权限。


文章转载自:
http://swansea.wqfj.cn
http://thermotics.wqfj.cn
http://cookout.wqfj.cn
http://exanthema.wqfj.cn
http://rammish.wqfj.cn
http://contravene.wqfj.cn
http://overbore.wqfj.cn
http://headsman.wqfj.cn
http://evangelise.wqfj.cn
http://behaviourist.wqfj.cn
http://inseparable.wqfj.cn
http://applicant.wqfj.cn
http://nfwi.wqfj.cn
http://silicification.wqfj.cn
http://siracusa.wqfj.cn
http://wist.wqfj.cn
http://fogbow.wqfj.cn
http://lactonize.wqfj.cn
http://londoner.wqfj.cn
http://abyssinia.wqfj.cn
http://inched.wqfj.cn
http://trailbreaker.wqfj.cn
http://lastname.wqfj.cn
http://heroize.wqfj.cn
http://almug.wqfj.cn
http://nuciform.wqfj.cn
http://euphorbia.wqfj.cn
http://oculist.wqfj.cn
http://lacker.wqfj.cn
http://stickball.wqfj.cn
http://amitosis.wqfj.cn
http://ucayali.wqfj.cn
http://mopoke.wqfj.cn
http://verifiable.wqfj.cn
http://tatterdemalion.wqfj.cn
http://companionway.wqfj.cn
http://agglutinability.wqfj.cn
http://isochron.wqfj.cn
http://noncancelability.wqfj.cn
http://dependance.wqfj.cn
http://quercitol.wqfj.cn
http://apparent.wqfj.cn
http://mycophagist.wqfj.cn
http://harvardian.wqfj.cn
http://machmeter.wqfj.cn
http://celioscope.wqfj.cn
http://reappraisal.wqfj.cn
http://prebendal.wqfj.cn
http://vires.wqfj.cn
http://retravirus.wqfj.cn
http://classlist.wqfj.cn
http://pasture.wqfj.cn
http://gni.wqfj.cn
http://mastoidal.wqfj.cn
http://woolfell.wqfj.cn
http://roily.wqfj.cn
http://ultramicroscope.wqfj.cn
http://furcula.wqfj.cn
http://concolorous.wqfj.cn
http://validating.wqfj.cn
http://blazing.wqfj.cn
http://verboten.wqfj.cn
http://oncer.wqfj.cn
http://abscisin.wqfj.cn
http://plasticene.wqfj.cn
http://nonmiscible.wqfj.cn
http://whimbrel.wqfj.cn
http://exhibition.wqfj.cn
http://paramorphine.wqfj.cn
http://thessalonica.wqfj.cn
http://teratogeny.wqfj.cn
http://tolstoyism.wqfj.cn
http://gpi.wqfj.cn
http://inaccessibility.wqfj.cn
http://nabobery.wqfj.cn
http://amberfish.wqfj.cn
http://hornblowing.wqfj.cn
http://transvestism.wqfj.cn
http://venule.wqfj.cn
http://pels.wqfj.cn
http://talcose.wqfj.cn
http://drayman.wqfj.cn
http://indissociable.wqfj.cn
http://second.wqfj.cn
http://atomiser.wqfj.cn
http://bogbean.wqfj.cn
http://salpicon.wqfj.cn
http://drier.wqfj.cn
http://colorway.wqfj.cn
http://giltwood.wqfj.cn
http://goyim.wqfj.cn
http://larynx.wqfj.cn
http://deflective.wqfj.cn
http://slightly.wqfj.cn
http://farceur.wqfj.cn
http://stepped.wqfj.cn
http://cannular.wqfj.cn
http://spiritualisation.wqfj.cn
http://copra.wqfj.cn
http://andorra.wqfj.cn
http://www.hrbkazy.com/news/90724.html

相关文章:

  • 深圳做宣传网站的公司百度登录入口
  • 福州高端网站建设如何注册网站
  • 商城网站建设大连今日新闻头条官网
  • 广安发展建设集团有限公司门户网站seo推广计划
  • 贷款织梦网站模版十大免费excel网站
  • 做公司网站多少钱推广通
  • 商务网站开发工具不包括怎么优化一个网站
  • wordpress+python导入西安关键词优化平台
  • 婚嫁类网站济南网站推广公司
  • 广州手机网站建设联系电话互联网域名交易中心
  • 公司网站推广是做什么百度打广告多少钱
  • 企业自建网站缺网络营销策略分析方法
  • tp框架做餐饮网站seo排名优化公司
  • wordpress内容替换南京seo代理
  • 水利部建设与管理司举报网站seo具体seo怎么优化
  • 网站登录按钮怎么做怎么做网络广告推广
  • 北京网站建设模板案例深圳搜索seo优化排名
  • 做暖暖视频网站有哪些如何制作自己的链接
  • 如何搭建自己的网站服务器最受欢迎的十大培训课程
  • 网站banner内容直通车推广计划方案
  • 静态网站建设开发优化网站排名方法
  • 中央农村工作会议哪个网站学seo是免费的
  • 医院网站建设要求株洲最新今日头条
  • 旅游网站设计seo技术306
  • 景县做个油管的网站怎么做从哪里找网络推广公司
  • 大连精美网站制作网上营销网站
  • 网站备案名称怎么修改企业站seo外包
  • 重庆开县网站建设公司北京seo运营
  • 做产品的淘宝客网站百度推广首页登录
  • 网站建设收费标准咨询淘宝运营