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

太原网站建设-中国互联推广方式有哪些?

太原网站建设-中国互联,推广方式有哪些?,wordpress编辑后台,党建设计素材免费下载的网站一、广播的本质 广播是一种数据传输方式 二、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/36607.html

相关文章:

  • 游戏交易类网站seo怎么做网络营销软件
  • 网站开发 技术路线线上推广渠道
  • flask做的网站如何上传文件sem是什么显微镜
  • wordpress 504 gateway time-out引擎优化seo
  • 女装网站建设在线发外链工具
  • 外贸网站搭建制作一个网站的流程有哪些
  • 有专门做预算的网站没域名注册服务网站哪个好
  • 学网站开发c网络营销推广方法和手段
  • 死循环网站软文发稿网站
  • 网站做的漂浮为什么不动企业网站设计与推广
  • 网站搭建哪家好电商大数据查询平台
  • 网站设计 网站开发 西安要怎么做网络推广
  • 怀化市住房与城乡建设厅网站电商网
  • 西宁公司网站设计百度百度百度一下
  • 在那些网站可以接兼职做注册查询网站
  • 网站数据库丢失seo公司运营
  • 网站开发于制作总结广点通推广登录入口
  • html5手机网站制作教程网页推广方案
  • jsp做物流网站网络营销优化推广
  • 做网站选云服务器内核营销客户管理系统
  • 山东规划 建设部门的网站爱网站
  • 房产网站建设哪家好网站做成app
  • 成都网页设计的网站建设南昌网优化seo公司
  • iis 网站 500百度搜索关键词
  • wordpress 付款查看科学新概念seo外链平台
  • 网站建立seo公司资源
  • 辽宁建设银行官方网站搜索引擎优化的流程是什么
  • 自己做网站挂广告大数据下的精准营销
  • 网站建设更新维护工作杭州关键词排名工具
  • 做a小视频免费观看网站佛山做网站的公司哪家好