BroadcastReceiver register 广播的动态注册方式
来源:互联网 发布:gm300写频软件 编辑:程序博客网 时间:2024/04/30 11:23
1.动态注册方式特点:在代码中进行注册后,当应用程序关闭后,就不再进行监听。
broadcast_test.xml
------------------------------------------------------------------------------------
AndroidManifest.xml
下面是具体的例子:
BroadcastTest.java
package com.czz.test;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class BroadcastTest extends Activity {private static final String ACTION = "com.czz.test.SENDBROADCAST";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.broadcast_test);Button btnSendBroadcast = (Button) this.findViewById(R.id.send);final Button btnRegisteBroadcast = (Button) this.findViewById(R.id.registe);final Button btnUnregisteBroadcast = (Button) this.findViewById(R.id.unregiste);btnRegisteBroadcast.setEnabled(true);btnUnregisteBroadcast.setEnabled(false);btnSendBroadcast.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsendBroadcast(new Intent(ACTION));}});btnRegisteBroadcast.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntentFilter filter = new IntentFilter();filter.addAction(ACTION);registerReceiver(myReceiver, filter);btnRegisteBroadcast.setEnabled(false);btnUnregisteBroadcast.setEnabled(true);}});btnUnregisteBroadcast.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubunregisterReceiver(myReceiver);btnRegisteBroadcast.setEnabled(true);btnUnregisteBroadcast.setEnabled(false);}});}private BroadcastReceiver myReceiver = new BroadcastReceiver(){@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubToast.makeText(context, "myReceiver receive", Toast.LENGTH_SHORT).show();}};}
broadcast_test.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="0px" android:visibility="gone" android:src="@drawable/earth"/><LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Broadcast test" android:singleLine="true" android:gravity="center_horizontal" /><Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send broadcast" android:layout_marginLeft="20px" /><Button android:id="@+id/registe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="registReceiver" android:layout_marginLeft="20px" android:layout_marginTop="20px" /><Button android:id="@+id/unregiste" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="unregistReceiver" android:layout_marginLeft="20px" android:layout_marginTop="20px" /></LinearLayout></FrameLayout>
------------------------------------------------------------------------------------
2.静态注册方式是在AndroidManifest.xml的application里面定义receiver并设置要接收的action。静态注册方式的特点:不管改应用程序是否处于活动状态,都会进行监听,比如某个程序时监听内存 的使用情况的,当在手机上安装好后,不管改应用程序是处于什么状态,都会执行改监听方法中的内容。
下面是具体的例子:
MyReceiver.java
public class MyReceiver extends BroadcastReceiver{ //定义日志标签 private static final String TAG = "Test"; @Override public void onReceive(Context context, Intent intent){ //输出日志信息 Log.i(TAG, "MyReceiver onReceive--->"); } }
AndroidManifest.xml
com.android.broadcast.RECEIVER_ACTION是你activity中的ACTION
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.broadcast" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <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="MyReceiver"> <intent-filter> <action android:name="com.android.broadcast.RECEIVER_ACTION"/> </intent-filter> </receiver> </application> </manifest>
- BroadcastReceiver register 广播的动态注册方式
- BroadCastReceiver的动态注册方式.txt
- BroadcastReceiver广播接收者的两种注册方式
- BroadcastReceiver广播接收者的两种注册方式
- BroadcastReceiver广播接收者的两种注册方式
- BroadcastReceiver静态注册广播与动态注册广播接收器
- Android:BroadcastReceiver广播注册方式汇总
- BroadcastReceiver探讨之动态广播注册流程
- Android 广播BroadcastReceiver静态/动态注册,获取系统电量广播【开机广播】,获取网络状态广播,Timer的使用简介
- BroadcastReceiver广播接听器的初步认识,动态注册,静态注册(1)
- Android BroadcastReceiver的注册方式静态和动态
- 内部类广播BroadcastReceiver的静态注册
- 动态注册的广播
- BroadcastReceiver的两种注册方式(静态注册和动态注册)
- BroadcastReceiver的两种注册方式(静态注册和动态注册)
- BroadcastReceiver的两种注册方式(静态注册和动态注册)
- BroadcastReceiver的两种注册方式(静态注册和动态注册)
- BroadcastReceiver的两种注册方式(静态注册和动态注册)android开发教程
- ICMP协议笔记
- 合并两个数组内容到一个新数组
- 使用kohana修改http404错误返回提示页面
- 创建XPCOM组件 2. 概述
- GSoC2011 主要入选方向
- BroadcastReceiver register 广播的动态注册方式
- volatile介绍(转载)
- RouterPassView – 路由密码查看器
- Android下按扭的使用方法
- 分词中的HMM
- 下面这个函数TX_memset4是什么意思了?
- 详解VS2005模块定义文件(.def)
- 学生类 总分 平均分 最高分 和 最低分的 java代码
- 布局的控件存在,但代码中无法实例化