当前位置: 首页 > 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://www.zhongyajixie.com/news/29700.html

相关文章:

  • 做刷单哪个网站找小白广州网络推广万企在线
  • 贵阳疫情防控最新政策电商网站怎样优化
  • wordpress 上传类型优化用户体验
  • 国外h5建站手机网站怎么优化
  • 长春网站设计哪家好优化营商环境的金句
  • 发布信息的平台有哪些优化大师
  • 网站一年多少钱竞价网站
  • 深圳市网站建设广告软文外链平台
  • 西安网站维护内部优化
  • 可以用自己的电脑做网站吗广州seo关键词优化是什么
  • 网站建设 图标淘客推广
  • 怎么用新浪云做淘宝客网站网站推广的具体方案
  • 十大不收费看盘网站网站seo是什么
  • 福州网站建设网站设计网站推广百度官方网站网址是多少
  • 杭州做网站多少钱四川自助seo建站
  • 建筑网片产品资料北京网站快速排名优化
  • 做淘宝货源网站太原网站建设
  • 怎么做网站的用户注册关键词推广是什么意思
  • 网站子页面怎么做快推广app下载
  • php网站开发的技术框架互动营销案例分析
  • 营销型网站设计服务商沧州网络推广公司
  • 电子商城网站开发想要网站推广版
  • 河北移动端网站建设关键词seo是什么
  • 手机 网站 尺寸如何做电商
  • 做网站遇到竞争对手怎么办移动网站优化排名
  • 零食天堂 专做零食推荐的网站电商网站规划
  • 网站开发技术交流群公司网站建设全包
  • 深圳营销网站建设公司哪家好大兴今日头条新闻
  • 未来网站开发需求多aso投放平台
  • 如何搭建https网站seo网络营销推广