Android广播的两种注册方式

Android广播机制概述 
Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器)。广播作为Android组件间的通信方式,可以使用的场景如下: 
1.同一app内部的同一组件内的消息通信(单个或多个线程之间); 
2.同一app内部的不同组件之间的消息通信(单个进程); 
3.同一app具有多个进程的不同组件之间的消息通信; 
4.不同app之间的组件之间消息通信; 
5.Android系统在特定情况下与App之间的消息通信。

静态注册:

定义一个广播接收器继承BroadcastReceiver

package com.example.administrator.broadcastdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {       
        Toast.makeText(context,intent.getStringExtra("info"),Toast.LENGTH_SHORT).show();
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

AndroidManifest.xml

  <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
        <!-- 静态注册广播 -->
        <!-- intent过滤器,指定可以匹配哪些intent, 一般需要定义action 可以是自定义的也可是系统的 -->  
        <intent-filter>
        <!--action-->
            <action android:name="com.broadcast.test" />
       </intent-filter>
 </receiver>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

MainActivity.class

package com.example.administrator.broadcastdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //静态注册广播
        Intent intent=new Intent();
        //与清单文件的receiver的anction对应
        intent.setAction("com.broadcast.test");
        intent.putExtra("info","测试静态注册广播");
        //发送广播
        sendBroadcast(intent);
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

动态注册: 
定义一个广播接收器继承BroadcastReceiver

package com.example.administrator.broadcastdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class DynamicReceiver extends BroadcastReceiver {
    public DynamicReceiver () {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context,intent.getStringExtra("name"),Toast.LENGTH_SHORT).show();
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Main2Activity.class

package com.example.administrator.broadcastdemo;

import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
    DynamicReceiver dynamicReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //动态注册广播
        dynamicReceiver = new DynamicReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.broadcast.test2");
        registerReceiver(dynamicReceiver, intentFilter);
        //发送信息
        Intent intent=new Intent();
        intent.setAction("com.broadcast.test2");
        intent.putExtra("name", "动态注册广播");
        sendBroadcast(intent);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除广播
        unregisterReceiver(dynamicReceiver);
    }
 }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

AndroidManifest.xml

  <receiver
     android:name=".DynamicReceiver"
     android:enabled="true"
     android:exported="true">
  </receiver>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

总结:

1)静态注册:在AndroidManifest.xml注册,android不能自动销毁广播接收器,也就是说当应用程序关闭后,还是会接收广播。 
2)动态注册:在代码中通过registerReceiver()手工注册.当程序关闭时,该接收器也会随之销毁。当然,也可手工调用unregisterReceiver()进行销毁。

android:enabled: 
这个属性用于定义系统是否能够实例化这个广播接收器,如果设置为true,则能够实例化,如果设置为false,则不能被实例化。默认值是true。 
<application>元素有它自己的enabled属性,这个属性会应用给应用程序的所有组件, 
包括广播接收器。<application><receiver>元素的这个属性都必须是true,这个广播接收器才能够被启用。如果有一个被设置为false,该广播接收器会被禁止实例化。 
android:exported: 

这个属性用于指示该广播接收器是否能够接收来自应用程序外部的消息,如果设置true,则能够接收,如果设置为false,则不能够接收。如果设置为false,这该接收只能接收那些由相同应用程序组件或带有相同用户ID的应用程序所发出的消息。

原文来自于 http://blog.csdn.net/zhiwenyan/article/details/50729844

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值