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

怎么做网站广告位企业网站怎么管理系统

怎么做网站广告位,企业网站怎么管理系统,出名的设计网站,做网站用什么后缀格式做好广播分为两种类型#xff1a;标准广播和有序广播 我们来看一下具体这两者的具体区别#xff1a; 1、发送标准广播 我们需要先定义一个广播接收器来准备接收此广播才行#xff0c;否则也是白发。 新建一个MyBroadcastReceiver,代码如下#xff1a; package com.example.broa…广播分为两种类型标准广播和有序广播 我们来看一下具体这两者的具体区别 1、发送标准广播 我们需要先定义一个广播接收器来准备接收此广播才行否则也是白发。 新建一个MyBroadcastReceiver,代码如下 package com.example.broadcasttest;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast;/*** Created by ZHJ on 2018/3/11.*/public class MyBroadcastReceiver extends BroadcastReceiver {Overridepublic void onReceive(Context context, Intent intent) {Toast.makeText(context,received in MyBroadcastReceiver,Toast.LENGTH_SHORT).show();} } 这里当MyBroadcastReceiver收到自定义的广播时就会弹出“received in MyBroadcastReceiver ”的提示。然后在AndroidManifest.xml中对这个广播接收器进行修改 ?xml version1.0 encodingutf-8? manifest xmlns:androidhttp://schemas.android.com/apk/res/androidpackagecom.example.broadcasttestuses-permission android:nameandroid.permission.ACCESS_NETWORK_STATE /uses-permission android:name android.permision.RECEIVE_BOOT_COMPLETED/applicationandroid:allowBackuptrueandroid:iconmipmap/ic_launcherandroid:labelstring/app_nameandroid:roundIconmipmap/ic_launcher_roundandroid:supportsRtltrueandroid:themestyle/AppThemeactivity android:name.MainActivityintent-filteraction android:nameandroid.intent.action.MAIN /category android:nameandroid.intent.category.LAUNCHER //intent-filter/activityreceiver android:name.MyBroadcastReceiverandroid:enabledtrueandroid:exportedtrueintent-filter action android:namecom.example.broadcasttest.MY_BROADCAST//intent-filter/receiver/application/manifest 可以看到这里让MyBroadcastReceiver接收一条值为com.example.broadcasttest.MY_BROADCAST的广播因此待会在发送广播的时候我们就需要发出这样的一条广播。 修改activity_main.xml中的代码如下 ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:contextcom.example.broadcasttest.MainActivityButtonandroid:idid/buttonandroid:layout_widthmatch_parentandroid:layout_heightwrap_content android:textSend Broadcast//LinearLayout 我们在布局中添加了一个按钮用于作为发送广播的触发点。 然后修改MainActivity中的代码 package com.example.broadcasttest;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;public class MainActivity extends AppCompatActivity {private IntentFilter intentfiletr;private NetworkChangeReceiver networkChangeReceiver;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);....Button button (Button)findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View view) {Intent intent new Intent(com.example.broadcasttest.MY_BROADCAST);sendBroadcast(intent);}});}.... }可以看到我们在按钮的点击事件里面加入了发送自定义广播的逻辑首先构建出了一个Intent对象并把要发送的广播的值传入然后调用了Context的sendBroadcast()方法将广播发送出去这样监听com.example.broadcasttest.MY_BROADCAST这条广播的广播接收器会收到消息。此时发出去的广播就是一条标准广播。运行程序点击按钮。回顾如何发送一条标准广播首先你得有个广播接收器那我们就新建一个广播接收器MyBroadcastReceiver然后在里面添加一个Toast,用于接收后广播用于反馈但是我们要在AndroidManifest.xml文件中对这个广播接收器进行修改你要接收什么样得广播。广播接收器就差不多做好了。我们开始准备发送广播添加一个按钮作为触发点在按钮的点击事件中添加发送自定义广播的逻辑。首先肯定要构建出Intent对象把要发送的广播的值传入然后调用Context的sendBroadcast()方法将广播发送出去。这样所有监听com.example.broadcasttest.MY_BROADCAST这条广播的广播接收器就会收到消息。这就是一条标准广播。另外广播是使用Intent进行传递的因此你还可以在Intent中携带一些数据传递给广播接收器。2、发送有序广播广播是一种跨进程的通信方式我们在应用程序内发出去的广播其他应用程序也是可以接收的。废话不说我们要验证新建BroadcastTest2项目。当然我们还需要在这个项目中新建一个广播接收器用于接收上一次的自定义广播新建AnotherBroadcastReceiver代码如下 package com.example.broadcasttest2;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast;/*** Created by ZHJ on 2018/3/11.*/public class AnotherBroadcastReceiver extends BroadcastReceiver {Overridepublic void onReceive(Context context, Intent intent) {Toast.makeText(context,received AnotherBroadcastReceiver,Toast.LENGTH_SHORT).show();} } 我们仍然是在广播接收器的onReceive()方法中弹出了一段文本信息。然后AndroidManifest.xml中对这个广播接收器进行修改代码如下 ?xml version1.0 encodingutf-8? manifest xmlns:androidhttp://schemas.android.com/apk/res/androidpackagecom.example.broadcasttest2applicationandroid:allowBackuptrueandroid:iconmipmap/ic_launcherandroid:labelstring/app_nameandroid:roundIconmipmap/ic_launcher_roundandroid:supportsRtltrueandroid:themestyle/AppThemeactivity android:name.MainActivityintent-filteraction android:nameandroid.intent.action.MAIN /category android:nameandroid.intent.category.LAUNCHER //intent-filter/activityreceiver android:name.AnotherBroadcastReceiverintent-filteraction android:namecom.example.broadcasttest.MY_BROADCAST//intent-filter/receiver/application/manifest 可以看到AndroidBroadcastReceiver接收的仍然是com.example.broadcasttest.MY_BROADCAST这条广播把BroadcastTest2运行起来点击BroadcastTest1的按钮那么你会接收两条提示信息。 这就证明了我们的应用程序是可以被其他的应用程序接收到的。 发送有序广播 到现在为止我们程序中发送的都是标准广播接下来我们来发送有序广播重新回到Broadcast项目然后修改MainActivity中的代码如下所示 package com.example.broadcasttest;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;public class MainActivity extends AppCompatActivity {private IntentFilter intentfiletr;private NetworkChangeReceiver networkChangeReceiver;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intentfiletr new IntentFilter();intentfiletr.addAction(android.net.conn.CONNECTIVITY_CHANGE);networkChangeReceiver new NetworkChangeReceiver();registerReceiver(networkChangeReceiver,intentfiletr);Button button (Button)findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View view) {Intent intent new Intent(com.example.broadcasttest.MY_BROADCAST);//注意这里的值一定要和BroadCastTest中的一样。sendOrderedBroadcast(intent,null);//这里由sendBroadcast()方法变化。}});}........ } 只是将sendBroadcast()方法改成sendOrderBroadcast()方法sendOrderBroadcast()方法接收两个参数第一个参数仍然是Intent第二个参数是一个与权限相关的字符串这里传入null就行了。重新运行程序这两个应用程序仍然可以接收到这条广播。但是这时候的广播接收器是有先后顺序的而且前面的广播接收器还可以将广播截断以阻止其传播。 那么该如何设定广播接收器的先后顺序呢当然是在注册的时候进行设定的修改AndroidManifest.xml中的代码 如下 ?xml version1.0 encodingutf-8? manifest xmlns:androidhttp://schemas.android.com/apk/res/androidpackagecom.example.broadcasttestuses-permission android:nameandroid.permission.ACCESS_NETWORK_STATE /uses-permission android:name android.permision.RECEIVE_BOOT_COMPLETED/applicationandroid:allowBackuptrueandroid:iconmipmap/ic_launcherandroid:labelstring/app_nameandroid:roundIconmipmap/ic_launcher_roundandroid:supportsRtltrueandroid:themestyle/AppThemeactivity android:name.MainActivityintent-filteraction android:nameandroid.intent.action.MAIN /category android:nameandroid.intent.category.LAUNCHER //intent-filter/activityreceiverandroid:name.MyBroadcastReceiverandroid:enabledtrueandroid:exportedtrueintent-filter android:priority100//我们给广播接收器设置了优先级action android:namecom.example.broadcasttest.MY_BROADCAST//intent-filter/receiver/application/manifest 可以看到我们通过android:priority属性给广播接收器设置了优先级优先级高的广播接收器就可以先收到广播这里将MyBroadcastReceiver的优先级设成100以保证它一定会在AnotherBroadcastReceicer之前收到广播。 既然我们已经获得了接收广播的优先权那么MyBroadCastReceiver就可以选择时候允许广播继续传递了。 修该MyBroadcastReceiver中的代码如下 package com.example.broadcasttest;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast;/*** Created by ZHJ on 2018/3/11.*/public class MyBroadcastReceiver extends BroadcastReceiver {Overridepublic void onReceive(Context context, Intent intent) {Toast.makeText(context,received in MyBroadcastReceiver,Toast.LENGTH_SHORT).show(); abortBroadcast();//调用abortBroadcast()方法。} } 如果在onReceive()方法中调用了abortBroadcast()方法就表示这条广播截断优先级的广播接收器就无法收到这条广播。 重新运行程序。 只有MyBroadcastReceiver中的广播接收器的Toast信息框可以弹出。
http://www.pierceye.com/news/283798/

相关文章:

  • 部队网站模板计算机网站建设员
  • 对象储存做网站微博内网站怎么做的
  • 运城做网站要多少钱谷歌网站英文
  • 校园网站建设的意见与建议做儿童交互网站
  • 7黄页网站建设网站建设培训会讲话
  • 百度推广公司地址苏州优化方式
  • 做一个电商网站建设银行网站打不开用什么浏览器
  • 保定住房和城乡建设局网站沙洋网站定制
  • 北京电脑培训网站网站首页怎么做全屏swf
  • 网站建设 设计 优化 维护爱站网关键词挖掘工具
  • 做电影收费网站二级域名查询
  • 销售网站模板a5站长网网站交易
  • 网站需要怎么做的吗做营销网站那个好
  • 苏州网站建设软件收费广东网站设计哪家专业
  • 中国产品网免费网站网站自定义功能实现
  • 做微信小程序和做网站短视频素材下载网站
  • 自治区住房和城乡建设厅网站自己怎么健网站视频教程
  • 昆明建站网址dw怎么做秋季运动会网站
  • 为什么要建设个人网站在建工程
  • o2o网站设计方案做一个网站只做前端怎么做
  • 长沙网站建设公司联系方式网站注册手机号安全吗
  • 广州市网站建设服务机构建设部网站查资质
  • 医院网站建设思路wordpress mx主题
  • 天津如何做百度的网站虚拟机做局域网网站服务器
  • 网站建设维护需要懂哪些知识网站建设优质公司
  • 怎么做网络彩票网站校园网站建设经费申请报告
  • 廊坊公司做网站一般网站图标是用什么做的
  • php网站开发文档模板玖壹购网站是做啥子的
  • 海报模板网站有哪些小程序电商平台排名
  • 百度一下百度网站苏州优秀网站设计企业