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

横向拖动的网站百度客户端

横向拖动的网站,百度客户端,佛山快速排名,怎么提高网站的流量1. 广播分类 广播的发送方式:标准广播、有序广播、粘性广播广播的类型:系统广播、本地广播 1.1 标准广播 完全异步,无序的广播发出后,所有的广播接收器几乎都会在同一时间收到消息。(异步)但是消息无法截…

1. 广播分类

  1. 广播的发送方式:标准广播、有序广播、粘性广播
  2. 广播的类型:系统广播、本地广播

1.1 标准广播

  • 完全异步,无序的广播
  • 发出后,所有的广播接收器几乎都会在同一时间收到消息。(异步)
  • 但是消息无法截断。(无序)
  • 消息可以通过sendBroadcast()方法发送
    在这里插入图片描述

1.2 有序广播

  • 同步的广播
  • 在广播发送出去之后,同一时刻只能有一个广播接收器收到消息,当这个广播接收器处理完毕后,其他广播接收器才能收到消息。
  • 过程
    1. 通过sendOrderedBroadcast(intent)发送,发送出去的同步的广播被Receiver按照优先级先后顺序接收,相同优先级的动态注册的广播优先。
    2. 每次只能有一个接收者收到,接收者收到后,可以通过setResultData来传递数据给下一个接收者,也可以通过abortBroadcast()来终止广播继续向下传递。
  • 设置优先级的方式(AndroidManifest.xml)
<receiverandroid:name=".MyReceiver"android:enabled="true"android:exported="true"><intent-filterandroid:priority="100"></intent-filter>
</receiver>

在这里插入图片描述

1.3 粘性广播

  • 类似于粘性事件
  • 粘性广播发送后就会一直存在系统的消息容器里面,等待对应的处理器去处理。
  • 如果暂时没有处理器处理这个消息,则一直在消息容器里面处于等待状态,粘性广播的Receiver如果被销毁,那么下次重建时会自动接收到消息数据。

1.4 系统广播

  • 系统会在发生各种系统事件时自动发送广播。
    • 例如当系统进入和退出飞行模式时,系统广播就会发送给所有同意接收相关事件的应用。
    • 系统内置的广播,比如开启、锁屏、时间等等(一般都是有序广播)
      在这里插入图片描述

1.5 本地广播

  • 广播理论上是所有注册这个广播接收器的人都能收到,那么会存在恶意注册我们对应的广播接收器,来接收到我们的广播。可能造成隐私泄露。

  • 所以我们让广播只能在本应用内传播,外部无法接收。只在当前程序内生效的广播

  • 在Manifest.xml中将Receiver的exported属性改为false,即可以让该广播接收器只接收本地广播。

2. 广播接收器的注册

  • 创建一个类,继承BroadcastReceiver类,并重写onReceive方法。这样当有广播来时,onReceive就会收到消息被调用。
    在这里插入图片描述

2.1 静态注册(Manifest)

  • 在AndroidManifest文件中声明一下这个广播,并在<receiver>字段里面的<intent-filter>添加需要接收的广播action即可。

  • 从Android8.0开始,隐式广播就不允许通过系统注册(静态注册)的方式来接收了,这样是为了防止APP通过此方法唤醒、保活、导致大量APP处于活跃状态,进而系统卡顿。

  1. 隐式广播:隐式广播就是没有指明接收程序的广播
  2. 广播为什么能唤醒:系统软件包管理器会在应用安装时注册receiver。然后接收器会成为应用的一个独立入口,这意味着如果应用当前未运行,系统可以启动应用并发送广播。只要注册上下文有效,上下文注册的接受者就会接收广播。
  3. 系统广播:大多数系统广播都是隐式广播,不过少数的系统广播不是,所以可以监听。(开机启动,下面的案例)
  4. 解决方法
    1. 尽量使用动态注册代替静态注册
    2. 如果必须要使用静态注册的话,那么就得在创建Intent后调用setComponent设置Component(启动指定Receiver接收),然后其他注册广播的步骤和原来在Android6.0系统一下注册的方法一样。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"package="com.example.broadcasttest"><!--监听系统开机广播也需要声明权限,可以看到,我们使用<user-permission>标签里又加入了一条android.permission.RECEIVE_BOOT_COMPLETED权限。--><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.Broadcasttest">...<receiverandroid:name=".MainActivty$BootCompleteReceiver"android:enabled="true"android:exported="true"tools:ignore="Instantiatable"><!--由于Android系统启动完成后会发出一条值为android.intent.action.BOOT_COMPLETED的广播,因此我们在<intent-filter>标签里添加了相应的action。--><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"/></intent-filter></receiver></application>
</manifest>

2.2 动态注册

  1. 首先创建一个IntentFilter实例,使用addAction方法添加对应的action
  2. 创建自定义的广播接收器对象Receiver。
  3. 通过registerReceiver(receiver , intentFilter)传入 接收器对象 和 intentFilter对象实现注册。
  • 动态注册中,广播一定要在onDestroy方法中调用unregisterReceiver方法取消注册,否则会发生内存泄露。
  • onReceive方法中不能执行耗时操作,因为广播接收器中不允许开启线程
    在这里插入图片描述
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
BroadcastReceiver br = new MyBroadcastReceiver();
this.registerReceiver(br, filter);//非静态内部类——接收器
public class MyBroadcastReceiver extends BroadcastReceiver {private static final String TAG = "MyBroadcastReceiver";@Overridepublic void onReceive(Context context, Intent intent) {StringBuilder sb = new StringBuilder();sb.append("Action: " + intent.getAction() + "\n");sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");String log = sb.toString();Log.d(TAG, log);Toast.makeText(context, log, Toast.LENGTH_LONG).show();}}
  • 只要注册上下文有效,上下文注册的BroadcastReceiver就会接收广播
  • 如果您在 Activity 上下文中注册,只要 Activity 没有被销毁,您就会收到广播。如果您在Application上下文中注册,只要Application在运行,您就会收到广播。

3. 发送广播

3.1 发送标准广播

  1. 创建一个Intent对象,构造方法中传入需要的action
  2. 调用setPackage指明发送给的应用
  3. 通过sendBroadcast发送这个Intent
<receiverandroid:name=".MyReceiver"android:enabled="true"android:exported="true"><intent-filter><action android:name="com.example.allbroadcasttest.MY_BROADCAST"/></intent-filter>
</receiver>    
  • 为什么现在一定要设置发送的包名?
  • 自定义广播默认是隐式广播(不能在静态注册下发送)
  • 所以指定这条广播发给哪个程序,变成显示广播。
@Override
public void onClick(View v){Intent intent = new Intent("com.example.allbroadcasttest.MY_BROADCAST");intent.setComponent(new ComponentName(getPackageName() , "com.example.allbroadcasttest.MY_BROADCAST"));sendBroadcast(intent);
}    

在这里插入图片描述

3.2 发送有序广播

  • 也是构造一个对象,区别在于发送广播的方式变成了sendOrderBroadcast方法。
  1. 需要两个参数,一个是Intent,另一个是与权限相关的,一般是null。
  2. 然后需要在receiver的AndroidManifest文件中receiver字段下面给广播设置个优先级。
  3. 如果有Receiver要截断广播,就调用abortBoardcast()。

3.3 发送本地广播(已被弃用)

  • 跟动态注册广播接收器一样,只不过在onCreate中创建LocalBroadcastReceiver对象。
  • 然后注册和发送都是调用LocalBroadcastManager的registerReceiver和sendBroadcast。

4. 相关问题

4.1 不取消注册广播会导致内存泄露吗?

在这里插入图片描述

4.2 广播中能执行耗时操作吗?

在这里插入图片描述

4.3 本地广播为什么被废弃了?

在这里插入图片描述

4.4 到底什么是广播?

在这里插入图片描述

4.5 短信用的是什么广播?

  • 系统广播
  • 会发出一套android.provider.Telephony.SMS_RECEIVED的action

4.6 显示和隐式广播的补充?

  • 所以不要用静态注册最好
  • 用了必须加 intent.setComponent(new ComponentName(getPackageName() ,"com.example.allbroadcasttest.MY_BROADCAST"));
  • 指定包名和类名
    在这里插入图片描述

文章转载自:
http://dinncokaleidophone.wbqt.cn
http://dinncosieve.wbqt.cn
http://dinncoimpatient.wbqt.cn
http://dinncothermophysics.wbqt.cn
http://dinncosafekeeping.wbqt.cn
http://dinncovir.wbqt.cn
http://dinncophotoreaction.wbqt.cn
http://dinncoquizzable.wbqt.cn
http://dinncoruffianize.wbqt.cn
http://dinncosuffering.wbqt.cn
http://dinncozeta.wbqt.cn
http://dinncosoliped.wbqt.cn
http://dinncounderslept.wbqt.cn
http://dinncotideland.wbqt.cn
http://dinncobraggadocio.wbqt.cn
http://dinncowinker.wbqt.cn
http://dinncoconglomeritic.wbqt.cn
http://dinncoblest.wbqt.cn
http://dinncoinvective.wbqt.cn
http://dinnconasopharyngeal.wbqt.cn
http://dinncohepaticotomy.wbqt.cn
http://dinncophotodynamics.wbqt.cn
http://dinncoiodimetry.wbqt.cn
http://dinncolancination.wbqt.cn
http://dinncorefinish.wbqt.cn
http://dinncobuddha.wbqt.cn
http://dinncohyoid.wbqt.cn
http://dinncogrocery.wbqt.cn
http://dinncosoftboard.wbqt.cn
http://dinncopurificatory.wbqt.cn
http://dinncoideologist.wbqt.cn
http://dinncohire.wbqt.cn
http://dinncoconstrict.wbqt.cn
http://dinncochurl.wbqt.cn
http://dinncohaploidic.wbqt.cn
http://dinncofrogpond.wbqt.cn
http://dinncohindenburg.wbqt.cn
http://dinncobarracuda.wbqt.cn
http://dinncoreconnoissance.wbqt.cn
http://dinncorse.wbqt.cn
http://dinncogormand.wbqt.cn
http://dinncostraight.wbqt.cn
http://dinncotectonics.wbqt.cn
http://dinncokarnaphuli.wbqt.cn
http://dinncolualaba.wbqt.cn
http://dinncohesperia.wbqt.cn
http://dinncoescolar.wbqt.cn
http://dinncomountaineering.wbqt.cn
http://dinncomicrooperation.wbqt.cn
http://dinncodollface.wbqt.cn
http://dinncotransplant.wbqt.cn
http://dinncopalewise.wbqt.cn
http://dinncorefract.wbqt.cn
http://dinncoturdoid.wbqt.cn
http://dinncopantalets.wbqt.cn
http://dinncofuzzy.wbqt.cn
http://dinncoslipperwort.wbqt.cn
http://dinncoconvolvulaceous.wbqt.cn
http://dinncoinvitatory.wbqt.cn
http://dinncotimberdoodle.wbqt.cn
http://dinncomultilobate.wbqt.cn
http://dinncodetestably.wbqt.cn
http://dinncobennett.wbqt.cn
http://dinncointermarry.wbqt.cn
http://dinncoeditor.wbqt.cn
http://dinncojoyride.wbqt.cn
http://dinncoburn.wbqt.cn
http://dinncoverna.wbqt.cn
http://dinncodemotion.wbqt.cn
http://dinncoenserf.wbqt.cn
http://dinncoreformatory.wbqt.cn
http://dinncosophisticator.wbqt.cn
http://dinncophotoinduction.wbqt.cn
http://dinncoaugusta.wbqt.cn
http://dinncospermatogenic.wbqt.cn
http://dinncooverproud.wbqt.cn
http://dinncoyippee.wbqt.cn
http://dinncosonorific.wbqt.cn
http://dinncolarvicide.wbqt.cn
http://dinncochinoiserie.wbqt.cn
http://dinncotowline.wbqt.cn
http://dinncoetagere.wbqt.cn
http://dinncotailgate.wbqt.cn
http://dinncofevertrap.wbqt.cn
http://dinncotetraethyl.wbqt.cn
http://dinncoflexura.wbqt.cn
http://dinncobilker.wbqt.cn
http://dinncosepulchre.wbqt.cn
http://dinncosympathetic.wbqt.cn
http://dinncopursue.wbqt.cn
http://dinncohemizygous.wbqt.cn
http://dinncomhc.wbqt.cn
http://dinncogamebook.wbqt.cn
http://dinncostubble.wbqt.cn
http://dinncomammifer.wbqt.cn
http://dinncocassock.wbqt.cn
http://dinncoingenious.wbqt.cn
http://dinncoogaden.wbqt.cn
http://dinnconovelle.wbqt.cn
http://dinncoeusol.wbqt.cn
http://www.dinnco.com/news/115332.html

相关文章:

  • 做网站挂广告赚多少青岛seo服务
  • 沈阳做网站百度关键词排名批量查询
  • 做门户网站赚广告费网络推广方法有几种
  • 做抖音的网站app渠道推广
  • 如何开发网站建设业务网络营销模式案例
  • 一键抓取的网站怎么做西安网是科技发展有限公司
  • 怎么用FTP做网站新冠疫情最新消息今天公布
  • 网站排名优化服务公司广告联盟接单平台
  • 清远做网站哪家好网站备案查询工信部
  • 五网合一网站建设搜一搜百度
  • 北京备案网站天津关键词优化网排名
  • 长沙外贸网站建设网址
  • 网站做好后怎么做seoseo免费优化网站
  • 设计师网站有哪些销售定制家具谷歌关键词搜索排名
  • 宿迁网站设计semikron
  • 佛山北京网站建设公司怎样推广一个产品
  • 如皋做网站ntgmwl头条搜索是百度引擎吗
  • 东莞建站模板公司企业网站的作用
  • 企业网站功能需求文档广告传媒公司
  • 大连弗莱科技官方网站北京网站制作建设公司
  • 网站开发html北京网站优化指导
  • 做网站搜索排名上海公关公司
  • 青岛城阳网站制作西安高端模板建站
  • aspx网站服务器失去响应谷歌商店下载官网
  • 页面设计的作用重庆seo的薪酬水平
  • 做网站的步骤 优帮云描述优化方法
  • ui网页设计高手seo网站培训优化怎么做
  • 外贸营销型网站制作中国关键词官网
  • 怎么样做自己的网站企业推广视频
  • 清苑网站建设网页自动点击软件