当前位置: 首页 > 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.zqtcl.cn/news/283798/

相关文章:

  • 网站建设的结论沭阳县建设局网站
  • 镇江网站制作价格网络有限公司简介
  • 海淀网站建设哪家公司好wordpress非常卡
  • 门户网站的建设意义交互设计专业就业前景
  • 那里有学做网站的2345网址导航下载官网
  • 房产证查询系统官方网站购买网站域名
  • 高端企业门户网站建设服务公司深圳企业网站怎么做
  • 页游网站如何做推广平面图设计软件有哪些
  • 自建网站有哪些wordpress 评论增加字段
  • 企业网站建设的方案书pc网站 公众号数据互通
  • 东莞设计制作网站制作做的asp网站手机号码
  • 必须做网站等级保护网站软件免费下载安装
  • 广州天河 网站建设上海招标网站
  • 云南网站建设方案专业的徐州网站开发
  • 政务服务 网站 建设方案郑州网站建设公司电话多少
  • 优化网站浏览量怎么看建设网站公司专业服务
  • php做的网站预览单产品网站建设
  • 网站文件验证上海推广网站公司
  • 如何免费申请网站外贸工艺品网站建设
  • 有名的wordpress网站网站开发企业培训
  • 中国建设银行绑定网站南宁seo如何做
  • 饮食类网站律师资格证报考条件
  • 昆明网站建设推广房源管理免费系统
  • jsp网站开发书籍环保网站 怎么做
  • 深圳营销型网站建设公司搜狗短网址生成
  • 如何优化购物网站建设广州seo公司排行
  • iis5.1 新建网站舆情系统的作用
  • 北京国互网网站建设公司东莞寮步搬家公司
  • 学校门户网站是什么意思做网站的意义大不大
  • 做网站卖酒网站内容建设的布局和结构