分类:C#、Android、VS2015;

创建日期:2016-02-29

一、简介

Android系统和你自己编写的应用程序都可以通过Indent发送和接收广播信息。广播的内容既可以是自定义的信息,也可以是Android的系统信息。例如,网络连接变化、电池电量变化、系统设置变化、接收到新的短信、微信、……等。

1、发送广播消息

一般都是通过后台服务中向前台发送广播消息。当然,也可以在某个Activity中向其他的Activity发送广播,不过,这种情况很少见。

要通过服务发送广播,可先定义一个继承自Service的子类,例如:

[Service]

public class MyService : Service

{

……

}

在该服务子类中,重写StartCommandResult()方法,然后在该方法中创建一个Intent实例,同时还可以调用这个Intent实例的PutExtra()方法包含要发送的附加消息(可选),最后调用SendBroadcast()方法,即可将该消息广播出去。例如:

intent = new Intent(action);

intent.PutExtra("message", "这是来自广播的消息");

SendBroadcast(intent);

注意:

(a)用Intent发送广播时,必须在参数中使用“全局唯一的标识符”指定该Intent要执行的动作,总之,你只要确保这个Action名称在整个项目中是唯一的就行了。

在实际应用中,动作字符串一般用“解决方案名+项目名+动作名”来表示,例如:

public static readonly string action = "MyBroadcastDemo.action1";

也可以用“自定义的命名空间+动作名”来表示。例如:

public static readonly string action = "www.cnblogs.com.rainmj.MyBroadcastDemo";

但是,这些都是一些约定的大家觉得比较好的做法,并不是必须这样做。换言之,唯一的要求就是不要在同一个项目中有相同的Action名称就行,这就是“全局唯一的标识符”的含义。

(b)由于发送广播一般都是通过后台服务来实现的,为了不让后台服务影响前台的界面操作,可以通过Task或者Thread来发送(见示例)。当然,如果服务用时很短,比如仅发送一个简单的字符串,那就没有必要用Task或者Thread来实现了。

发送广播的完整代码见示例中的ch1501Service.cs文件。

2、接收广播消息

对于广播接收端来说,凡是注册了BroadcastReceiver的应用程序类,都可以从继承自BroadcastReceiver的类中接收到广播消息,这个实现接收广播的BroadcastReceiver子类就叫广播接收器。

广播接收器(Broadcast Receivers)接收的广播内容一般来自Android内置的标准服务,另外,还可以来自你自己定义的服务。

例如:

public class MyBroadcastReceiver : BroadcastReceiver

{

……

}

广播接收器一旦启动,就会自动监听你注册的广播,并在OnReceive()方法中接收广播消息。因此,要正常接收广播,必须在该子类中重写OnReceive()方法,例如:

public override void OnReceive(Context context, Intent intent)

{

……

}

默认情况下,Android要求OnReceive方法必须在5秒内执行完毕,否则Android会认为该组件失去响应,并提示用户强行关闭该组件。

接收广播的完整代码见示例中的ch1501BroadcastReceiver.cs文件。

3、注册接收的广播地址

一般在Activity中注册要用哪个广播接收器来接收广播。

有两种注册接收广播地址的方式,一种是通过C#代码实现(常用),另一种是在配置文件AndroidManifest.xml中实现(不常用)。

通过C#代码实现的办法示例如下:

var receiver = new MyBroadcastReceiver();

RegisterReceiver(receiver, new IntentFilter(MyService.action));

StartService(new Intent(this, typeof(MyService)));

这段代码注册一个BroadcastReceiver,并通过IntentFilter(Intent过滤器)指定要执行的动作,然后就可以调用StartService()方法启动广播服务。

调用RegisterReceiver()后,系统就会自动在配置文件(AndroidManifest.xml)中配置Intent过滤器,不需要我们自己在这个文件中去手动配置。

二、示例1运行截图

本示例演示如何定时发送多种类型的广播。通过这个例子,相信你应该能真正理解“要发送的动作(Action)”的含义,同时也应该能明白如何在一个接收器中通过Intent过滤器注册多个动作。

该例子提前使用了下一章将要介绍的Android Service的功能。

三、主要设计步骤

1、添加ch1501_Main.axml文件

在Resource/layout文件夹下添加该文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/ch1501_btnStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动服务(发送广播)" />
<Button
android:id="@+id/ch1501_btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止服务(停止广播)" />
<TextView
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp" />
</LinearLayout>

2、添加ch1501Service文件

在SrcDemos文件夹下添加该文件,模板选择【Class】。

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using System;
using System.Threading.Tasks; namespace MyDemos.SrcDemos
{
[Service]
public class ch1501Service : Service
{
public const string action1 = "www.cnblogs.com.rainmj.ch1501Service";
public const string action2 = "MyDemos.ch1501Service.Info1";
public const string action3 = "MyDemos.ch1501Service.Info2";
public const string action4 = "MyDemos.ch1501Service.Info3";
public static bool IsCancel; [return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
//一般应根据不同的情况通过action发送不同的广播信息,
//如果仅发送一条广播,像下面这样直接发送即可,不需要用Task.Run实现
//intent = new Intent(action1);
//SendBroadcast(intent); //如果是长时间运行的后台服务,为了不影响界面操作,最好用
//线程来实现,比如发送后台处理的进度等。
//下面以每隔1秒随机选取一个action为例,来演示长时间运行
//的服务以及定时向前台发送不同类型广播的办法
Task.Run(async () =>
{
Random r = new Random();
string[] actions = { action1, action2, action3, action4 };
string action = actions[0];
while (true)
{
if (IsCancel == true)
{
break;
}
intent = new Intent(action);
SendBroadcast(intent);
action = actions[r.Next(actions.Length)];
await Task.Delay(1000);
}
});
return StartCommandResult.Sticky;
} public override IBinder OnBind(Intent intent)
{
return null;
}
}
}

3、添加ch1501BroadcastReceiver.cs文件

在SrcDemos文件夹下添加该文件,模板选择【Class】。

using Android.Content;
using Android.Widget; namespace MyDemos.SrcDemos
{
public class ch1501BroadcastReceiver : BroadcastReceiver
{
private TextView txt;
public ch1501BroadcastReceiver(ch1501MainActivity activity)
{
txt = activity.FindViewById<TextView>(Resource.Id.textView1);
} public override void OnReceive(Context context, Intent intent)
{
//此处应该对接收到的intent.Action分别进行处理
//为简单起见,例子仅将这个字符串在界面上显示出来了
txt.Text += "\n收到:" + intent.Action;
}
}
}

4、添加ch1501MainActivity.cs文件

在SrcDemos文件夹下添加该文件,模板选择【Activity】。

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget; namespace MyDemos.SrcDemos
{
[Activity(Label = "【例15-1】广播基本用法")]
public class ch1501MainActivity : Activity
{
private ch1501BroadcastReceiver receiver;
private Intent intent;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch1501_Main);
var txt = FindViewById<TextView>(Resource.Id.textView1); IntentFilter intentFilter = new IntentFilter();
intentFilter.AddAction(ch1501Service.action1);
intentFilter.AddAction(ch1501Service.action2);
intentFilter.AddAction(ch1501Service.action3);
intentFilter.AddAction(ch1501Service.action4);
receiver = new ch1501BroadcastReceiver(this);
RegisterReceiver(receiver, intentFilter); intent = new Intent(this, typeof(ch1501Service));
var btnStart = FindViewById<Button>(Resource.Id.ch1501_btnStart);
btnStart.Click += delegate
{
txt.Text = "";
ch1501Service.IsCancel = false;
StartService(intent);
};
var btnStop = FindViewById<Button>(Resource.Id.ch1501_btnStop);
btnStop.Click += delegate
{
ch1501Service.IsCancel = true;
StopService(intent);
};
} protected override void OnDestroy()
{
UnregisterReceiver(receiver);
ch1501Service.IsCancel = true;
StopService(intent);
base.OnDestroy();
}
}
}

【Android】15.2 广播的更多相关文章

  1. 【Android】15.0 第15章 广播和通知—本章示例主界面

    分类:C#.Android.VS2015: 创建日期:2016-02-28 一.简介 广播(Broadcast):其功能类似于收音机的广播,你只要调到那个台(只要在接收的类中注册了要接收的广播),就能 ...

  2. Android之旅---广播(BroadCast)

    什么是广播 在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.我们拿广播电台来做个比方.我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们 ...

  3. Android开发之广播

    广播是Android开发中的一个重要的功能,在Android里面有各式各样的广播,比如:电池的状态变化.信号的强弱状态.电话的接听和短信的接收等等,现在给大家简单介绍一下系统发送.监听这些广播的机制. ...

  4. android之自定义广播

    布局文件 点击按钮发送广播 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...

  5. Android 常见的广播 action常量

    Intent.ACTION_AIRPLANE_MODE_CHANGED; //关闭或打开飞行模式时的广播 Intent.ACTION_BATTERY_CHANGED; //充电状态,或者电池的电量发生 ...

  6. Android中的广播

    Android中的广播 广播接受器,可以比喻成收音机.而广播则可以看成电台. Android系统内部相当于已经有一个电台 定义了好多的广播事件,比如外拨电话 短信到来 sd卡状态 电池电量变化... ...

  7. Android中本地广播的实现

    其实Android的本地广播并没有什么好讲的,他就是用了一个localbroadcastmanager类来sendbroadcast,以及注册和注销广播,没有什么特点,其中实例该类的时候用了getin ...

  8. Android 中的广播(Broadcast)

    Android 广播(broadcast) 饮水思源 本文章内容学习和总结自 郭霖大神:<Android第一行代码> Overview 就像我们的学校里的喇叭一样,是用来通知的.而Andr ...

  9. Android 中的广播机制

    Android 中的广播机制 Android 中的广播,按照广播响应范围,可以分为应用内广播和全局广播.按照广播的接收方式,可以分为标准广播和有序广播. 广播的分类 响应范围 应用内广播:此类广播只能 ...

  10. Android为什么需要广播Broadcast

       在Android系统中,为什么需要广播机制呢?广播机制,本质上它就是一种组件间的通信方式,如果是两个组件位于不同的进程当中,那么可以用Binder机制来实现,如果两个组件是在同一个进程中,那么它 ...

随机推荐

  1. MNI模板和Talairach 模板的对比

    The MNI brain and the Talairach atlas SPM 96 and later use standard brains from the Montreal Neurolo ...

  2. telnet用法 测试端口号

    Telnet是进行远程登录的标准协议和主要方式它为用户提供了在本地计算机上完成远程主机工作的能力.可以用telnet命令来测试端口号是否正常打开还是关闭状态. 工具/原料 电脑 cmd命令 方法/步骤 ...

  3. iOS开源项目:DYNavigationController

    DYNavigationController是一个实现了左右滑动导航的项目. https://github.com/dyang/DYNavigationController 首先用之前的跟视图初始化D ...

  4. iOS:本地数据库sqlite的介绍

    一.数据库的概念: 1..什么是数据库 SQL Server 2010.Oracle.MySQL 关系数据库 NoSQL数据库-非关系型数据库   数据库主要由表组成 表由字段组成 数据 就是表中的记 ...

  5. Scurm 术语

    角色 Product Owner Scrum Master Team 工件(Backlog) Product Backlog Sprint Backlog Burndown Backlog 活动 Sp ...

  6. 自动签到升级版(JS实现的每日定时任务)

    公司规定每日签到两次:日子太安逸了,有时候中午居然会忘记签到…… 于是,笔者寻思写一个自动签到的脚本:每天指定两个签到时段,每次打开页面,先检测当前是否为签到时段,如果在签到时段,则检查cookie中 ...

  7. js中的this基础

    this在js中的地位可以说是相当高了,本文介绍下this的基本相关情况,以后还会慢慢介绍 在页面中aler(this)//this的指向是window 在DOM操作中this的指向是当前发生事件的对 ...

  8. C++如何禁止掉对象的复制操作

    最容易想到的是将拷贝构造函数与赋值函数声明为private.但是,private只是说外部不能直接调用,但是可以间接通过类的成员函数与友元函数对其访问.那么怎么办呢? ---->在类中,允许声明 ...

  9. Python mongoHelper模块

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' Defines a MongoOperator class and allows you to m ...

  10. [ES6] 03. The let keyword -- 1

    var message = "Hi"; { var message = "Bye"; } console.log(message); //Bye The mes ...