BroadcastReceiver register 广播的动态注册方式
2011-12-01 11:16
411 查看
1.动态注册方式特点:在代码中进行注册后,当应用程序关闭后,就不再进行监听。
下面是具体的例子:
BroadcastTest.java
broadcast_test.xml
------------------------------------------------------------------------------------
2.静态注册方式是在AndroidManifest.xml的application里面定义receiver并设置要接收的action。静态注册方式的特点:不管改应用程序是否处于活动状态,都会进行监听,比如某个程序时监听内存 的使用情况的,当在手机上安装好后,不管改应用程序是处于什么状态,都会执行改监听方法中的内容。
下面是具体的例子:
MyReceiver.java
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"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.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(){ @Override public void onClick(View v) { // TODO Auto-generated method stub sendBroadcast(new Intent(ACTION)); } }); btnRegisteBroadcast.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub IntentFilter filter = new IntentFilter(); filter.addAction(ACTION); registerReceiver(myReceiver, filter); btnRegisteBroadcast.setEnabled(false); btnUnregisteBroadcast.setEnabled(true); } }); btnUnregisteBroadcast.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub unregisterReceiver(myReceiver); btnRegisteBroadcast.setEnabled(true); btnUnregisteBroadcast.setEnabled(false); } }); } private BroadcastReceiver myReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.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"> <ImageView android: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 广播的动态注册方式
- Android开发22——广播接收者BroadcastReceiver的原理和注册方式 推荐
- Android LocalBroadcastManager 注册广播和直接registerReceiver的区别
- Android LocalBroadcastManager 注册广播和直接registerReceiver的区别
- BroadcastReceiver静态注册广播与动态注册广播接收器
- Android LocalBroadcastManager 注册广播和直接registerReceiver的区别
- BroadcastReceiver广播接收者的两种注册方式
- BroadCastReceiver(广播接收者)动态注册简单例子
- Service实时向Activity传递数据案例,,,,BroadcastReceiver中的数据传递给activity(2中注册方式广播)
- Android开发22——广播接收者BroadcastReceiver的原理和注册方式
- Android开发学习笔记:BroadcastReceiver简介和注册方式
- Android应用程序注册广播接收器(registerReceiver)的过程分析
- Android(java)学习笔记173:BroadcastReceiver之 BroadcastReceiver静态注册(SD卡卸载或者安装案例)和 BroadcastReceiver动态注册注销
- Android应用程序注册广播接收器(registerReceiver)的过程分析
- Android应用程序注册广播接收器(registerReceiver)的过程分析
- Android的广播Receiver动态注册和静态注册示例
- Android应用程序注册广播接收器(registerReceiver)的过程分析
- Android之Broadcast Receiver的两种注册方式
- Mars Chen动态注册Broadcast广播监听器的实现
- android静态注册广播(BroadCastReceiver) Calling startActivity() from outside of an Activity