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

太原网站建设-中国互联外链seo推广

太原网站建设-中国互联,外链seo推广,wordpress mu下载,学校网站建设计入哪个会计科目一、广播的本质 广播是一种数据传输方式 二、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.khdw.cn/news/33294.html

相关文章:

  • 武昌做网站公司推荐建立一个网站需要多少钱?
  • 市网站开发公司自己可以创建网站吗
  • 网站运维服务内容百度手机助手下载免费安装
  • 创一个网站怎样赚钱网站推广是什么
  • 地方门户网站建设方案百度发作品入口在哪里
  • 运营商网站登录注册成都网站优化排名
  • 保定 网站建设湖南长沙今日疫情
  • 建行信用卡网站今日热搜
  • 企业网站建站费用谷歌浏览器网页
  • 网站本地可以打开海外营销推广服务
  • 即墨网站建设代理推广
  • 如何做网站方案优化软件seo排名
  • js素材网站长春seo技术
  • 赣州做网站的新手seo入门教程
  • 网站中的滚动照片怎么做手机黄页怎么找
  • 怎么做网站的登录界面电工培训机构
  • 人民法院公告网怎么查询被起诉seo搜索引擎优化是做什么的
  • 佛山微信网站设计百度百家号
  • 模板网站跟设计性网站的区别公司网址怎么注册
  • 网站项目怎么做的账号权重查询入口
  • 怎样做一个微信公众号北京seo如何排名
  • 论坛网站建设价格地推团队
  • 网站建设推广优化招聘模板就业seo好还是sem
  • 网站建设html代码如何添加济南网站设计
  • 乐陵新闻最新消息今天优化网站推广教程整站
  • 电商网站开发教学视频kj6699的seo综合查询
  • 苏州营销型网站建设推广seo入门书籍推荐
  • 网站怎么做分站seo关键词布局
  • 站酷网站的图是用什么做的扬州网站seo
  • 冯宗耀seo教程搜索引擎排名优化价格