BroadcastReceiver register 广播的动态注册方式

1.动态注册方式特点:在代码中进行注册后,当应用程序关闭后,就不再进行监听。

下面是具体的例子:

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>   

 

转载于:https://www.cnblogs.com/zhujiabin/p/7506645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值