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>   

 

posted @ 2017-09-11 19:50  星辰之力  阅读(516)  评论(0编辑  收藏  举报