大四实习准备5_android广播机制
2015-5-1
android 广播机制
5.1简介
分为标准广播(Normal broadcasts)(无先后顺序,几乎同时接收,不可截断)和有序广播(Ordered broadcasts)(有先后顺序,可以截断)两种。
5.2接收系统广播
广播接收器对感兴趣的广播进行注册,这样就能监听到对应的广播,并在内部处理相应的逻辑。
注册广播的方式有两种,分别为在代码中注册(动态注册)(缺点:必须在程序启动之后才能接收到广播,因为注册的逻辑是写在onCreat()方法中的)和在AndroidManifest.xml(静态注册)中注册。
创建广播接收器的方法为:新建一个继承自BroadcastReceiver的类,并重写父类的onReceive()方法就行了,监听到广播时对应的处理逻辑就在onReceive()方法中。
5.2.1通过动态注册的方法实现监听网络变化
IntentFilter:组件告诉Android系统自己乐意接收哪些隐式intent(显式的Intent会直接传送到目标组件)
http://blog.csdn.net/today520/article/details/7000048
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.widget.Toast; 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);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver,intentFilter);
} @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
} class NetworkChangeReceiver extends BroadcastReceiver{//非静态内部类 @Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if( networkInfo != null && networkInfo.isAvailable() ){
Toast.makeText(arg0, "network is availbale", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(arg0, "network is unavailbale", Toast.LENGTH_SHORT).show();
}
} }
}
MainActivity.class
需在在AndroidManifest.xml中声明
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
5.2.2通过静态注册监听开机启动广播
//开机启动广播只能用静态注册来监听(?)
package com.example.broadtest2; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class BootCompleted extends BroadcastReceiver{ @Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Toast.makeText(arg0, "开机啦",Toast.LENGTH_SHORT).show();
}
}
BootCompleted.class 广播接收器
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadtest2"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootCompleted" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>
AndroidManifest.xml 这里需对开机启动做权限声明&静态注册广播
这里MainActivity.class中不用写跟广播有关系的内容。
5.3发送自定义广播
5.3.1发送标准广播
Intent intent = new Intent("lalala");//自定义的广播名
intent.putExtra("key", "get lalala BroadCast~");//可以附带些数据
sendBroadcast(intent);
例子:采用静态注册的方法,点击按钮发送一个自定义的广播,MainActivity中就是发送了下广播,没有直接涉及到diy_broadcastReceiver.class。
package com.example.broadcasttest; import android.app.Activity;
import android.content.Intent;
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 { Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent("lalala");
intent.putExtra("key", "get lalala BroadCast~");
sendBroadcast(intent);
}
});
}
}
MainActivity.class
package com.example.broadcasttest; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class diy_broadcastReceiver extends BroadcastReceiver{ @Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String s = arg1.getExtras().getString("key");//或者arg1.getStringExtra("key");
Toast.makeText(arg0, s, Toast.LENGTH_SHORT).show();
} }
diy_broadcastReceiver.class
<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"
tools:context="${relativePackage}.${activityClass}" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="19dp"
android:text="Send BroadCast" /> </RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".diy_broadcastReceiver" >
<intent-filter>
<action android:name="lalala" />
</intent-filter>
</receiver>
</application> </manifest>
AndroidManifest.xml
别的应用,如果静态注册了同样的广播,也会进行相应的操作。
问题:
一个广播接收器注册了多个广播,怎样根据广播来源的不同,进行不同的操作?
5.3.2发送有序广播
//..........
大四实习准备5_android广播机制的更多相关文章
- Android中使用广播机制退出多个Activity
谷歌百度一下,Android中退出多个Activity的方法,大家讨论的很多. 在实习的时候,看到公司的项目退出多个Activity,是采用LinkedList方法,毕业设计的时候,也参照了那种方法. ...
- Android随笔之——Android广播机制Broadcast详解
在Android中,有一些操作完成以后,会发送广播,比如说发出一条短信,或打出一个电话,如果某个程序接收了这个广播,就会做相应的处理.这个广播跟我们传统意义中的电台广播有些相似之处.之所以叫做广播,就 ...
- Android广播机制的深入学习
部分内容转载自http://www.cnblogs.com/lwbqqyumidi/p/4168017.html 1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者 ...
- Android总结篇系列:Android广播机制
1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...
- 九、Android学习第八天——广播机制与WIFI网络操作(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 九.Android学习第八天——广播机制与WIFI网络操作 今天熟悉了An ...
- Android 中的消息传递,详解广播机制
--------------------------------------广播机制简介--------------------------------------------- Android中的广 ...
- Android广播机制简介
为什么说Android中的广播机制更加灵活呢?这是因为Android中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容,这些广播可能是来自于系统的,也可能是来 ...
- Android广播机制概述
1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...
- Android广播机制:Broadcast
转载:Android总结篇系列:Android广播机制 1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广 ...
随机推荐
- php字符串首字母转换大小写的实例分享
php中对字符串首字母进行大小写转换的例子. in: 后端程序首字母变大写:ucwords() <?php $foo = 'hello world!'; $foo = ucwords($foo) ...
- 【WPF】Application应用程序启动
wpf应用程序在启动的时候会自动创建Main函数并调用Application实例的run(),从而启动Application进程.Main函数在一个App.g.cs文件中,App.g.cs文件的位置在 ...
- eclipse里maven项目An error occurred while filtering resources解决办法(转载)
转自:http://liyanjie918.blog.163.com/blog/static/20227290201581143110105/ 在使用eclipse构建maven项目时,突然出现错误提 ...
- Ubuntu Linux启用root用户登录
Ubuntu Linux有一个与众不同的特点,那就是初次使用时,你无法作为root来登录系统,为什么会这样?这就要从系统的安装说起.对于其他Linux系统来 说,一般在安装过程就设定root密码,这样 ...
- 自己实现的android树控件,android TreeView
1.开发原因 在项目中经常需要一个需要一个树状框架,这是非常常见的控件.不过可能是谷歌考虑到android是手机系统,界面宽度有限, 所以只提供了只有二级的ExpandableListView.虽然这 ...
- 1046: [HAOI2007]上升序列 - BZOJ
Description 对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ...
- Javascript字典操作
<script type="text/javascript"> var dic = new Array(); //注意它的类型是Array ...
- 团体程序设计天梯赛-练习集L1-011. A-B
L1-011. A-B 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你计算A-B.不过麻烦的是,A和B都是字符串 — ...
- CodeForces 32C
额 找找规律吧 要用long long 才过. #include <cstdio> #include <algorithm> using namespace std; in ...
- 浏览器对象模型(BOM)
BOM结构 用户浏览网页的时候,浏览器会自动创建一些对象,这些对象存放着浏览器窗口的属性和相关信息,也就是大家熟称的BOM.浏览器对象模型是一个层次化的对象集,我们可以通过window对象访问所有对象 ...