前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android中BroadcastReceiver广播

Android中BroadcastReceiver广播

作者头像
欢醉
发布2018-01-22 11:08:30
9160
发布2018-01-22 11:08:30
举报
文章被收录于专栏:james大数据架构james大数据架构

BroadCastReceiver 简介

广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadcast() 、 Context.sendOrderedBroadcast() 来实现的。通常一个广播 Intent 可以被订阅了此 Intent 的多个广播接收者所接收。 

广播是一种广泛运用的在应用程序之间传输信息的机制 。而 BroadcastReceiver 是对发送出来的广播进行过滤接收并响应的一类组件; 

来自普通应用程序,如一个应用程序通知其他应用程序某些数据已经下载完毕。

 BroadcastReceiver 自身并不实现图形用户界面,但是当它收到某个通知后, BroadcastReceiver 可以启动 Activity 作为响应,或者通过 NotificationMananger 提醒用户,或者启动 Service 等等。

生命周期

    一个BroadcastReceiver 对象只有在被调用onReceive(Context, Intent)的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。

    因此从这个特征可以看出,在所调用的onReceive(Context, Intent)函数里,不能有过于耗时的操作,不能使用线程来执行。对于耗时的操作,请start service来完成。因为当得到其他异步操作所返回的结果时,BroadcastReceiver 可能已经无效了。

使用BroadcastReceiver

编写类继承BroadcastReceiver,复写onReceiver()方法

代码语言:js
复制
package com.example.receive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceivceReg extends BroadcastReceiver {
	private static final String TAG = "MyReceivce";
	public MyReceivceReg(){
		Log.i(TAG,"MyReceivceReg");
 
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG,"onReceiveReg");
	}
}

在Manifest.xml中注册BroadcastReceiver

代码语言:javascript
复制
<receiver android:name="com.example.receive.MyReceivce">
            <intent-filter >
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>

要接收某些action,需要在AndroidManifest.xml里面添加相应的permission。例如接收SMS:

代码语言:javascript
复制
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
……
</manifest>

界面:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:orientation="vertical"
    tools:context=".AndroidBroadcastServiceActivity" >

    <Button
        android:id="@+id/btnStartBroad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动Broadservice"
         >
    </Button>
      
     <Button
        android:id="@+id/btnRegisterBroad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册RegisterBroad"
         >
    </Button>
    
      <Button
        android:id="@+id/btnUnRegisterBroad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消RegisterBroad"
         >
    </Button>

</LinearLayout>

构建Intent,发送

代码语言:javascript
复制
package com.example.androidbroadcastservice;

import com.example.receive.MyReceivceReg;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidBroadcastServiceActivity extends Activity {
    protected static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";//广播类型。其实是对应Manifest.xml中<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    private Button btnStartBroad,btnRegisterBroad,btnUnRegisterBroad;
    private MyReceivceReg receivece;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_broadcast_service);
        btnStartBroad = (Button) this.findViewById(R.id.btnStartBroad);
        btnStartBroad.setOnClickListener(onclick);

        btnRegisterBroad = (Button) this.findViewById(R.id.btnRegisterBroad);
        btnRegisterBroad.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                receivece=new MyReceivceReg();
                IntentFilter filter=new IntentFilter();
                filter.addAction(ACTION);
                registerReceiver(receivece, filter);
            }
        });
        
        btnUnRegisterBroad = (Button) this.findViewById(R.id.btnUnRegisterBroad);
        btnUnRegisterBroad.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //MyReceivceReg receivece=new MyReceivceReg();
                //IntentFilter filter=new IntentFilter(ACTION);
                unregisterReceiver(receivece);
            }
        });
    }
    private OnClickListener onclick = new OnClickListener() {

        @Override
        public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(ACTION);
                sendBroadcast(intent);
        }
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_android_broadcast_service,
                menu);
        return true;
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-03-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档