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

太原网站建设-中国互联吴中seo网站优化软件

太原网站建设-中国互联,吴中seo网站优化软件,营销型网站规划建设的七大要素,廊坊cms模板建站一、广播的本质 广播是一种数据传输方式 二、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://www.ds6.com.cn/news/121757.html

相关文章:

  • 网页设计网站架构今天的新闻大事10条
  • 怎样通过阿里巴巴网站开发客户网站的收录情况怎么查
  • 如何给网站做第三方流量监测怎么创造自己的网站
  • 从化建设局网站关停最新seo黑帽技术工具软件
  • 网上开平台要多少钱苏州seo按天扣费
  • 视频类的网站制作做网站的平台有哪些
  • 刷单类网站开发合肥网站推广优化
  • 本网站正在建设升级中深圳网站设计专家乐云seo
  • 免费网站建设空间2345网止导航
  • 深圳 倡导居家办公爱站网seo综合查询
  • 手机app开发语言seo168小视频
  • wordpress主题开发层级谷歌优化排名公司
  • 网站飘窗 两学一做免费域名注册平台
  • 长沙企业网站建设多少钱品牌推广和营销推广
  • 360老是提示危险网站网站优化方案设计
  • 淄博制作网站营销的手段和方法
  • 青岛网站设计如何做代写文章多少钱
  • 网站优化怎么做效果才好电商运营自学全套教程
  • 厦门外贸网站搭建友链网
  • wordpress中文游戏门户国外网站seo免费
  • 企业网站建设设计方案关键词优化排名公司
  • 沧州南皮网站建设大数据营销推广精准粉
  • 企慕网站建设网络推广广州网站设计建设
  • 一站式网站建设供应商汕头seo推广优化
  • 设计本官方网站广告关键词自动优化
  • 南京网站制作公司怎么样站长工具站长
  • 景县做网站一键建站
  • 广东品牌网站建设一个完整的产品运营方案
  • 可以做电算化的网站郑州seo询搜点网络效果佳
  • 男人和男人做爰漫画网站上海网站建设方案