(一)广播机制简介

  1、Android广播的分类:

  如图所示:

  

  2、发送广播:使用Intent;接收广播:Broadcast Receiver。

  (二)接收系统广播

  1、动态注册监听网络变化

  示例程序:

  (1)MainActivity(注:以下代码中的ToastUtil是自己简单封装的Toast显示功能的类):

 package com.example.broadcasttest;

 import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends Activity { private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 1.创建IntentFilter实例
intentFilter = new IntentFilter();
// 2.用addAction方法添加action
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); // 3.创建内部类NetworkChangeReceiver实例
networkChangeReceiver = new NetworkChangeReceiver();
// 4.注册
registerReceiver(networkChangeReceiver, intentFilter);
} class NetworkChangeReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// 创建ConnectivityManager实例
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// 创建NetworkInfo对象(需要申请权限ACCESS_NETWORK_STATE)
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo(); // 判断NetworkInfo的状态,即网络是否可用
if (networkInfo != null && networkInfo.isAvailable()) {
ToastUtil.showShort(MainActivity.this, "网络可用!");
} else {
ToastUtil.showShort(MainActivity.this, "网络不可用!");
} }
} @Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
} }

  (2)申请权限

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

  (3)xml文件:不需要添加什么内容。

  2、静态注册实现开机启动

  动态注册的一个缺点就是,必须要在程序启动之后才能接收到广播,而静态注册就可以在程序还未启动时就能接收到广播,利用这一点就可以实现诸如开机启动程序的功能。

  示例程序:

  (1)新建类BootCompleteReceiver继承自BroadcastReceiver(注:onReceive方法中红不能放过于耗时的逻辑,因为其中不允许使用线程):

 package com.example.broadcasttest;

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class BootCompleteReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
ToastUtil.showShort(context, "BroadcastTest开机启动");
} }

  (2)在AndroidManifest.xml静态注册广播:

 <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
...
<receiver android:name=".BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

  (3)申请权限:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

  (三)发送自定义广播

  1、发送标准广播

  (1)在BroadcastTest项目中:

  ①创建MyBroadcastReceiver:

 public class MyBroadcastReceiver extends BroadcastReceiver {

     @Override
public void onReceive(Context context, Intent intent) {
ToastUtil.showShort(context, "在MyBroadcastReceiver中接收到了自定义广播!");
} }

  ②在AndroidManifest.xml中注册广播接收器:

      <receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
</receiver>

  ③activity_main.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/send_broadcast_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送自定义广播" /> </LinearLayout>

  ④MainActivity:

 package com.example.broadcasttest;

 import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button sendBroadcast; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); sendBroadcast = (Button) findViewById(R.id.send_broadcast_btn);
sendBroadcast.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_broadcast_btn:
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
break;
default:
break;
}
}
}

  (2)创建BroadcastTest2项目,在其中:

  ①创建AnotherBroadcastReceiver:

 public class AnotherBroadcastReceiver extends BroadcastReceiver {

     @Override
public void onReceive(Context context, Intent intent) {
ToastUtil.showShort(context, "在AnotherBroadcastReceiver中接收到了自定义广播!");
} }

  ②在AndroidManifest.xml中注册广播接收器:

      <receiver android:name=".AnotherBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
</receiver>

  (3)同时运行BroadcastTest和BroadcastTest2程序,然后在BroadcastTest中点击“发送自定义广播”按钮,然后就会发现弹出两次Toast显示接收到了广播。

  2、发送有序广播

  在1中BroadcastTest项目的基础上,做以下修改即可(红色加下划线的代码为新增或修改的代码):

  (1)MainActivity中:

   @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_broadcast_btn:
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendOrderedBroadcast(intent, null);
break;
default:
break;
}
}

  (2)AndroidManifest.xml中:

      <receiver android:name=".MyBroadcastReceiver" >
<intent-filter android:priority="100" >
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
</receiver>

  (3)MyBroadcastReceiver类中:

 public class MyBroadcastReceiver extends BroadcastReceiver {

     @Override
public void onReceive(Context context, Intent intent) {
ToastUtil.showShort(context, "在MyBroadcastReceiver中接收到了自定义广播!");
abortBroadcast();
} }

  (4)再运行两个程序,点击发送广播按钮后,发现只看到了一个Toast提示,因为另一个广播接收被截断了。 

  (四)使用本地广播

  以上的广播都是全局广播,也就是任何应用程序都能接收到。而这会引发安全性问题,如果只希望在当前应用程序内部传递广播,就要使用本地广播了。

  本地广播的关键是使用LocalBroadcastManager来发送广播。示例程序:

  1、xml文件:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/send_broadcast_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送自定义广播" /> </LinearLayout>

  2、MainActivity:

 package com.example.broadcasttest;

 import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button sendBroadcast; private IntentFilter intentFilter; private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 1.获取localBroadcastManager实例
localBroadcastManager = LocalBroadcastManager.getInstance(this); sendBroadcast = (Button) findViewById(R.id.send_broadcast_btn); // 2.在点击事件中用LocalBroadcastManager的sendBroadcast方法发送广播
sendBroadcast.setOnClickListener(this); // 3.注册IntentFilter
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver, intentFilter);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_broadcast_btn:
Intent intent = new Intent(
"com.example.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);
break;
default:
break;
}
} @Override
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}
}

  3、注册广播接收器:

      <receiver android:name=".LocalReceiver" >
<intent-filter>
<action android:name="com.example.broadcasttest.LOCAL_BROADCAST" />
</intent-filter>
</receiver>

  4、这时如果也让另一个程序接收LOCAL_BROADCAST这个广播,会发现是接收不到的。

  5、本地广播的优点:

  (1)不用担心机密数据泄露。

  (2)其他程序无法将广播发送到我们程序的内容,不用担心安全漏洞的问题。

  (3)比全局广播更高效。

  (五)最佳实践——实现强制下线功能

  在登录页面输入账号密码进入主界面后,点击强制下线按钮会弹出强制下线Dialog,并且该Dialog不能被取消,当用户点击确定后会发出强制下线广播,再次跳转到登录界面。

  1、login.xml文件:

 <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" > <TableRow> <TextView
android:layout_height="wrap_content"
android:text="用户名:" /> <EditText
android:id="@+id/user_name_et"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</TableRow> <TableRow> <TextView
android:layout_height="wrap_content"
android:text="密码:" /> <EditText
android:id="@+id/password_et"
android:layout_height="wrap_content" >
</EditText>
</TableRow> <TableRow> <Button
android:id="@+id/login_bt"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="登录" />
</TableRow> </TableLayout>

  2、ActivityCollector类:

 public class ActivityCollector {
public static List<Activity> activities = new ArrayList<Activity>(); public static void addActivity(Activity activity) {
activities.add(activity);
} public static void removeActivity(Activity activity) {
activities.remove(activity);
} public static void finishAll() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}

  3、BaseActivity类:

 public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); ActivityCollector.addActivity(this);
} @Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}

  4、LoginActivity类:

 public class LoginActivity extends BaseActivity {

     private EditText userNameEt;
private EditText passwordEt;
private Button loginBt; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login); userNameEt = (EditText) findViewById(R.id.user_name_et);
passwordEt = (EditText) findViewById(R.id.password_et);
loginBt = (Button) findViewById(R.id.login_bt); loginBt.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
String userName = userNameEt.getText().toString();
String password = passwordEt.getText().toString(); // 如果用户名是admin且密码是123456,就认为登录成功
if (userName.equals("110") && password.equals("123456")) {
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

  5、activity_main.xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.broadcastbestpractice.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是主界面" /> <Button
android:id="@+id/force_offline_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:text="发送一个强制下线广播" /> </RelativeLayout>

  6、MainActivity类:

 public class MainActivity extends Activity {

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button forceOfflineBt = (Button) findViewById(R.id.force_offline_bt);
forceOfflineBt.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent(
"com.example.broadcastbestpractice.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
} }

  7、ForceOfflineReceiver:

 public class ForceOfflineReceiver extends BroadcastReceiver {

     @Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setTitle("警告");
dialogBuilder.setMessage("你将要被强制下线!请重新登录!");
dialogBuilder.setCancelable(false);
dialogBuilder.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
ActivityCollector.finishAll();
Intent intent = new Intent(context, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}); AlertDialog alertDialog = dialogBuilder.create();
alertDialog.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
}

  8、AndroidManifest.xml:

 ...
     <activity
android:name=".LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" >
</activity> <receiver
android:name=".ForceOfflineReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.example.broadcastbestpractice.FORCE_OFFLINE" />
</intent-filter>
</receiver>
...

 

 

随机推荐

  1. Redis入门经典——The Little Redis Book (翻译)

    The Little Redis Book By Karl Seguin 关于本书:本书全然免费下载.你能够任意转载,复制.但请你注明作者.Karl Seguin.译者,WY. 以及不要用于商业用途. ...

  2. JQuery IN ACTION读书笔记之一: JQuery选择器

    本章关注两个通过$()使用的常用功能: 通过选择器选择DOM元素,创建新DOM元素. 2.1 选择操作元素 JQuery采用了CSS的语法,而CSS的语法你可能已经很熟悉了.当然,JQuery也做了扩 ...

  3. jQuery 属性操作方法

    方法 描述 addClass() 向匹配的元素添加指定的类名. attr() 设置或返回匹配元素的属性和值. hasClass() 检查匹配的元素是否拥有指定的类. html() 设置或返回匹配的元素 ...

  4. 洛谷1006==codevs1169

    P1006 传纸条 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法 ...

  5. Disable Client Certificate Revocation (CRL) Check on IIS

    Disable Client Certificate Revocation (CRL) Check on IIS: REGISTRY  : HKLM\SYSTEM\CurrentControlSet\ ...

  6. GridView中给DropDownList动态绑定数据,及选择列表值后自动更新数据库

    protected void sgvFile1_RowDataBound(object sender, GridViewRowEventArgs e) { DropDownList ddlAM = ( ...

  7. 【转】虚拟 IO 服务器(VIOS)和 IBM i

    Power 主机上的虚拟化应用,简单阐述虚拟 IO 服务器的功能,用途,优点,以及虚拟 IO 服务器在高级虚拟化技术的作用.举例说明虚拟 IO 服务器与 IBM i 分区直接互联特性. 引言 随着信息 ...

  8. 整理前端css/js/jq常见问题及解决方法(3)

    jq: 1.prepend(参数);//将参数内容前置再某元素内部; eg: <div id="div1">奇妙能力歌</div> $("#div ...

  9. ES6学习笔记(三)——数值的扩展

    看到这条条目录有没有感觉很枯燥,觉得自己的工作中还用不到它所以实在没有耐心看下去,我也是最近得闲,逼自己静下心来去学习去总结,只有在别人浮躁的时候你能静下心来去学去看去总结,你才能进步.毕竟作为前端不 ...

  10. python列表和元组相互转换

    # 将列表转化为元组 lst=[11,22,33] t=tuple(lst) print(t,type(t)) # 打印结果:(11, 22, 33) <class 'tuple'> tu ...