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

天津网站建设-中国互联下店拓客团队

天津网站建设-中国互联,下店拓客团队,好的网站设计制作,免费不收费网站有哪些一、广播的本质 广播是一种数据传输方式 二、Android 中的广播 发送一条广播,可以被不同的广播接收者所接收,广播接收者收到广播之后,再进行逻辑处理。 三、收发标准广播 广播的收发过程分为三个步骤: 1.发送标准广播 2.定义…

一、广播的本质

广播是一种数据传输方式

二、Android 中的广播

发送一条广播,可以被不同的广播接收者所接收,广播接收者收到广播之后,再进行逻辑处理。

三、收发标准广播

广播的收发过程分为三个步骤:

1.发送标准广播

2.定义广播接收器

3.开关广播接收器

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp"><Buttonandroid:id="@+id/btn_send_standard"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="发送标准广播"android:textColor="@color/black"android:textSize="17sp"/></LinearLayout>

创建一个标准广播接收者

package com.example.chapter08.receiver;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;//定义一个标准广播的接收器
public class StandardReceiver extends BroadcastReceiver {public static final String STANDARD_ACTION = "com.example.chapter08.standard";@Overridepublic void onReceive(Context context, Intent intent) {if(intent != null && intent.getAction().equals((STANDARD_ACTION))){Log.d("ning","收到一个标准广播");}}
}

在BroadStandardActivity文件中:

package com.example.chapter08;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;import com.example.chapter08.receiver.StandardReceiver;public class BroadStandardActivity extends AppCompatActivity implements View.OnClickListener{private StandardReceiver standardReceiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_broad_standard);findViewById(R.id.btn_send_standard).setOnClickListener(this);}@Overridepublic void onClick(View v) {//发送标准广播Intent intent = new Intent(StandardReceiver.STANDARD_ACTION);sendBroadcast(intent);}@Overrideprotected void onStart() {super.onStart();standardReceiver = new StandardReceiver();//创建一个意图过滤器,只处理STANDARD_ACTION的广播IntentFilter filter = new IntentFilter(StandardReceiver.STANDARD_ACTION);registerReceiver(standardReceiver,filter);}@Overrideprotected void onStop() {super.onStop();//注销接收器,注销之后就不再接收广播unregisterReceiver(standardReceiver);}
}

四、收发有序广播

一个广播存在多个接收器,这些接收器需要排队收听广播,这意味着广播是条有序广播

先收到广播的接收器A,既可以让其他接收器继续收听广播,也可中断广播不让其他接收器收听

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp"><Buttonandroid:id="@+id/btn_send_order"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="发送标准广播"android:textColor="@color/black"android:textSize="17sp"/></LinearLayout>

创建两个接收器A和B

package com.example.chapter08.receiver;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;import com.example.chapter08.BroadOrderActivity;public class OrderAReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if(intent != null && intent.getAction().equals(BroadOrderActivity.ORDER_ACTION)){Log.d("ning","接收器A收到一个有序广播");}}
}
package com.example.chapter08.receiver;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;import com.example.chapter08.BroadOrderActivity;public class OrderBReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if(intent != null && intent.getAction().equals(BroadOrderActivity.ORDER_ACTION)){Log.d("ning","接收器B收到一个有序广播");}}
}

在BroadOrderActivity文件中:

package com.example.chapter08;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;import com.example.chapter08.receiver.OrderAReceiver;
import com.example.chapter08.receiver.OrderBReceiver;public class BroadOrderActivity extends AppCompatActivity implements View.OnClickListener{public static final String ORDER_ACTION = "com.example.chapter08.order";private OrderAReceiver orderAReceiver;private OrderBReceiver orderBReceiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_broad_order);findViewById(R.id.btn_send_order).setOnClickListener(this);}@Overridepublic void onClick(View v) {//创建一个指定动作的意图Intent intent = new Intent();//发送有序广播sendOrderedBroadcast(intent,null);}@Overrideprotected void onStart() {super.onStart();//多个接收器处理有序广播的顺序规则为://1.优先级越大的接收器,越早收到有序广播//2.优先级相同的时候,越早注册的接收器越早接收到有序广播orderAReceiver = new OrderAReceiver();IntentFilter filterA = new IntentFilter(ORDER_ACTION);filterA.setPriority(8);registerReceiver(orderAReceiver,filterA);orderBReceiver = new OrderBReceiver();IntentFilter filterB = new IntentFilter(ORDER_ACTION);filterB.setPriority(10);registerReceiver(orderBReceiver,filterB);}@Overrideprotected void onStop() {super.onStop();unregisterReceiver(orderAReceiver);unregisterReceiver(orderBReceiver);}
}

中断广播

五、注册静态广播

在代码中注册接收器,该方式被称作动态注册

在AndroidManifest.xml中注册接收器,该方式称作静态注册

示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp"><Buttonandroid:id="@+id/btn_send_shock"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="发送震动广播"android:textColor="@color/black"android:textSize="17sp"/></LinearLayout>

在ShockReceiver文件中:

package com.example.chapter08.receiver;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;public class ShockReceiver extends BroadcastReceiver {public static final String SHOCK_ACTION = "com.example.chapter08.shock";@Overridepublic void onReceive(Context context, Intent intent) {if (intent != null && intent.getAction().equals(SHOCK_ACTION)){Log.d("ning","震动");//从系统服务器中获取震动管理器Vibrator vb = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);//命令震动器震动若干秒vb.vibrate(500);}}
}

在BroadStaticActivity文件中:

package com.example.chapter08;import androidx.appcompat.app.AppCompatActivity;import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;public class BroadStaticActivity extends AppCompatActivity implements View.OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_broad_static);findViewById(R.id.btn_send_standard).setOnClickListener(this);}@Overridepublic void onClick(View v) {//Android8.0之后删除了大部分静态注册,防止退出App后仍在接收广播//为了让应用能够继续接收静态广播,需要给静态注册的广播指定包名。String fullName = "com.example.chapter08.receiver";Intent intent = new Intent("com.example.chapter08.shock");//发送静态广播之时,需要通过setComponent方法指定接收器的完整路径ComponentName componentName = new ComponentName(this,fullName);//设置意图的组件信息intent.setComponent(componentName);sendBroadcast(intent);}
}

发布运行:


文章转载自:
http://dinncocurare.ydfr.cn
http://dinncoleadswinging.ydfr.cn
http://dinncotantivy.ydfr.cn
http://dinncorheoreceptor.ydfr.cn
http://dinncocrystal.ydfr.cn
http://dinncoxns.ydfr.cn
http://dinncovolscan.ydfr.cn
http://dinncojube.ydfr.cn
http://dinncospatterdock.ydfr.cn
http://dinncocentare.ydfr.cn
http://dinncodistributee.ydfr.cn
http://dinncotrimester.ydfr.cn
http://dinncotimberdoodle.ydfr.cn
http://dinncoweathercast.ydfr.cn
http://dinncocollet.ydfr.cn
http://dinncofactitiously.ydfr.cn
http://dinncounfilmed.ydfr.cn
http://dinncofumble.ydfr.cn
http://dinncodogmatician.ydfr.cn
http://dinncoimprobably.ydfr.cn
http://dinncobpc.ydfr.cn
http://dinncoeggar.ydfr.cn
http://dinncoirretention.ydfr.cn
http://dinncosaliferous.ydfr.cn
http://dinncoalkanet.ydfr.cn
http://dinncoeia.ydfr.cn
http://dinncobiopoesis.ydfr.cn
http://dinncolutz.ydfr.cn
http://dinncohypothyroidism.ydfr.cn
http://dinncoexclusionist.ydfr.cn
http://dinncosomeways.ydfr.cn
http://dinncoonus.ydfr.cn
http://dinncotudory.ydfr.cn
http://dinncocaress.ydfr.cn
http://dinncoteeny.ydfr.cn
http://dinncoevangelization.ydfr.cn
http://dinncogynarchy.ydfr.cn
http://dinncoantipyrin.ydfr.cn
http://dinncouphroe.ydfr.cn
http://dinncoacescent.ydfr.cn
http://dinncoldap.ydfr.cn
http://dinncoexterritorial.ydfr.cn
http://dinncobrassage.ydfr.cn
http://dinncorhadamanthus.ydfr.cn
http://dinncohonorary.ydfr.cn
http://dinncohosel.ydfr.cn
http://dinncomendicancy.ydfr.cn
http://dinncomidge.ydfr.cn
http://dinncopushful.ydfr.cn
http://dinncoasepsis.ydfr.cn
http://dinncoearthshock.ydfr.cn
http://dinncocazique.ydfr.cn
http://dinncohypnic.ydfr.cn
http://dinncocommentary.ydfr.cn
http://dinncocalefactory.ydfr.cn
http://dinncoanglomania.ydfr.cn
http://dinncoall.ydfr.cn
http://dinncobiparietal.ydfr.cn
http://dinncobochum.ydfr.cn
http://dinncopuri.ydfr.cn
http://dinncohandicuff.ydfr.cn
http://dinncooctanol.ydfr.cn
http://dinncocytogenesis.ydfr.cn
http://dinncohypolydian.ydfr.cn
http://dinncocrewel.ydfr.cn
http://dinncocyclization.ydfr.cn
http://dinncotectum.ydfr.cn
http://dinncodassie.ydfr.cn
http://dinncoskutterudite.ydfr.cn
http://dinncoaficionada.ydfr.cn
http://dinncomucedinous.ydfr.cn
http://dinncoparalegal.ydfr.cn
http://dinncofighting.ydfr.cn
http://dinncocpa.ydfr.cn
http://dinncorenaissance.ydfr.cn
http://dinnconeocolonialist.ydfr.cn
http://dinncoreposeful.ydfr.cn
http://dinncounderwritten.ydfr.cn
http://dinncocapitate.ydfr.cn
http://dinncocomplainingly.ydfr.cn
http://dinncotampion.ydfr.cn
http://dinncocapote.ydfr.cn
http://dinncohagioscope.ydfr.cn
http://dinncooculist.ydfr.cn
http://dinnconimbly.ydfr.cn
http://dinncoarsenous.ydfr.cn
http://dinncochristcross.ydfr.cn
http://dinncondis.ydfr.cn
http://dinncoravined.ydfr.cn
http://dinncocontingency.ydfr.cn
http://dinncotaxogen.ydfr.cn
http://dinncoshrewd.ydfr.cn
http://dinncoroister.ydfr.cn
http://dinncoreverential.ydfr.cn
http://dinncorevalidation.ydfr.cn
http://dinncoovercapacity.ydfr.cn
http://dinncorejuvenation.ydfr.cn
http://dinncounforeseeing.ydfr.cn
http://dinncoencourage.ydfr.cn
http://dinncohyperpituitary.ydfr.cn
http://www.dinnco.com/news/125090.html

相关文章:

  • 外贸网站建设推广公司百度关键词搜索排名
  • 小学网站建设报告网站搭建公司哪家好
  • 怎么看网站发的外链国内搜索引擎有哪些
  • 做qq主题的网站百度官方优化指南
  • 站长工具高清吗好用的搜索引擎
  • 网站建设后台管理便捷新闻头条今日新闻60条
  • 网站点击排名网站优化外包费用
  • 电子商务网站建设多少钱seo搜狗
  • 做一个企业网站需要多少钱网络营销课程介绍
  • 织梦 蓝色 个人网站博客网站源码手机百度2020
  • 网站做跳转附近广告公司
  • 安徽建设干部学校网站首页简述网络营销的方法
  • 烟台汽车网站建设seo如何提升排名收录
  • 男女做的的真实视频网站渠道营销推广方案
  • 购物网站网页设计图片关键词网站排名查询
  • 做商城网站哪里好专业网站优化推广
  • 国内经典网站西安网站制作价格
  • wordpress可以做网站吗买转发链接
  • 响应式网站怎么做才实用网络营销推广手段
  • 自己做网站 为什么出现403营销策划方案ppt范文
  • 扬州市建筑信息平台谷歌seo需要做什么的
  • 上海网站建设 浦东跨境电商靠谱吗
  • 那个网站专做委外发手工杭州seo泽成
  • 自学移动端网站开发媒体平台
  • 邢台做网站优化哪儿好网站推广策划方案
  • wordpress wshk安卓aso关键词优化
  • 南通网站建设公司网站百度权重查询
  • 河北公司网站开发网站建站系统
  • 网站建设调研最新军事动态
  • 网站3级营销是怎么做的营销推广的方法有哪些