android 52 粘滞广播
粘滞广播:广播发送出去以后,广播接收者还没有创建,当广播接收者注册的时候就可以接收,如果不是粘滞广播则如果没有广播接收者就以后不能再接收了。


mainActivity:
package com.sxt.day07_07; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent("com.sxt.day07_07.my_receiver");
sendStickyBroadcast(intent);//发送粘滞广播,一直停留等着接收者
setListener();
} private void setListener() {
findViewById(R.id.btnStartSecondActivity).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2=new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent2);//启动SecondActivity
}
});
} }
SecondActivity
package com.sxt.day07_07; import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu; public class SecondActivity extends Activity {
MyReceiver mReceiver;
Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
registerReceiver();
}
private void registerReceiver() {
mReceiver=new MyReceiver();
IntentFilter filter=new IntentFilter("com.sxt.day07_07.my_receiver");
registerReceiver(mReceiver, filter);
} //内部类,只有SecondActivity启动了,MyReceiver才能注册接收广播。
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("main","MyReceiver.onReceive()");
mIntent=intent;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
removeStickyBroadcast(mIntent);//移出,移出以后就收不到广播了
unregisterReceiver(mReceiver);//移出接收者
}
}
系统描述文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxt.day07_07"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BROADCAST_STICKY"/> 粘滞广播要申请权限
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sxt.day07_07.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>
<activity
android:name="com.sxt.day07_07.SecondActivity"
android:label="@string/title_activity_second" >
</activity>
</application> </manifest>
main页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/btnStartSecondActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>
second页面:
<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=".SecondActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>
android 52 粘滞广播的更多相关文章
- Android 四大组件之三(广播)
1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...
- Android组件系列----BroadcastReceiver广播接收器
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- Android应用程序注册广播接收器(registerReceiver)的过程分析
前 面我们介绍了Android系统的广播机制,从本质来说,它是一种消息订阅/发布机制,因此,使用这种消息驱动模型的第一步便是订阅消息:而对 Android应用程序来说,订阅消息其实就是注册广播接收器, ...
- HTML四种定位-粘滞定位
粘滞定位 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset=&q ...
- Android开发学习—— Broadcast广播接收者
现实中:电台要发布消息,通过广播把消息广播出去,使用收音机,就可以收听广播,得知这条消息.Android中:系统在运行过程中,会产生许多事件,那么某些事件产生时,比如:电量改变.收发短信.拨打电话.屏 ...
- shift粘滞键后门创建/复原批处理
创建shift粘滞键后门: 1 c: 2 3 cd \Windows\System32\ 4 5 rename sethc.exe bak_sethc.exe 6 7 xcopy cmd.exe se ...
- 九、Android学习第八天——广播机制与WIFI网络操作(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 九.Android学习第八天——广播机制与WIFI网络操作 今天熟悉了An ...
- Android系统中的广播(Broadcast)机制简要介绍和学习计划
在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制:这些组件甚至是可以位于不同的进程中,这样它就像Binder机制一样,起到进程间通信的作用:本文通过一个 ...
- Linux中的特殊权限粘滞位(sticky bit)详解
Linux下的文件权限 在linux下每一个文件和目录都有自己的访问权限,访问权限确定了用户能否访问文件或者目录和怎样进行访问.最为我们熟知的一个文件或目录可能拥有三种权限,分别是读.写.和执行操作, ...
随机推荐
- OC语言-02面向对象的三大特性
01封装 #import <Foundation/Foundation.h> @interface Student : NSObject { //@public 成员变量尽量不使用 int ...
- 内存管理tcmalloc
tcmalloc https://code.google.com/p/gperftools/
- [BZOJ 1112] [POI2008] 砖块Klo 【区间K大】
题目链接:BZOJ - 1112 题目分析 枚举每一个长度为k的连续区间,求出这个区间的最优答案,更新全局答案. 可以发现,这个区间的所有柱子最终都变成这k个数的中位数时最优,那么我们就需要查询这个区 ...
- German Collegiate Programming Contest 2013:B
一个离散化的简单题: 我用的是STL来做的离散化: 好久没写离散化了,纪念一下! 代码: #include<cstdio> #include<cstring> #include ...
- 改善 ASP.NET MVC 代码库的 5 点建议
MVC,建议 刚刚检查完支持工单中的一些代码,笔者想针对 ASP.NET MVC 应用的改进写一些建议.这些内容仍在笔者脑海中,愿与各位一同分享.若你已使用 MVC 一段时间,那么以下内容可能并不新鲜 ...
- Internship
zoj2532:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1532 题意:有n个发射点,m个中继站,然后发射点会发射一些信息 ...
- 几个字符串的误区,以及setlocale函数的使用
转自 http://www.blogjava.net/baicker/archive/2007/08/09/135642.html 转自 http://witmax.cn/character-enco ...
- 协定类型不具有 ServiceContractAttribute 特性
协定类型 ZBMService.QueryHistoryData 不具有 ServiceContractAttribute 特性.若要定义有效协定,指定的类型(协定接口或服务类)必须具有 Servic ...
- 【转】MFC下拉列表框的用法
原文网址:http://blog.csdn.net/kinglimy/article/details/6452239 Combo Box (组合框)控件很简单,可以节省空间.从用户角度来看,这个控件是 ...
- PowerDesigner将PDM导出生成WORD文档--温习老知识
转:http://www.cnblogs.com/wudiwushen/archive/2010/05/13/1734812.html 今天的温习老知识,是如何将一个PD设计的PDM来导出WORD文档 ...