码迷,mamicode.com
首页 > 移动开发 > 详细

淘宝(阿里百川)手机客户端开发日记第六篇 广播机制详解(一)

时间:2015-07-01 20:26:44      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

public abstract class BroadcastReceiver;

Base class for code that will receive intents sent by sendBroadcast().

If you don‘t need to send broadcasts across applications, consider using this class with LocalBroadcastManager instead of the more general facilities described below. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml

注册广播,有2中方法:

1.  在Manifest.xml文件中注册,需要一个android:name设置,所以我们继承BroadcastReceiver的类需要单独作为一个类文件出现,而在java代码中注册,我们可以使用一个内部类就可以了。

2.  在Mainfest.xml文件中注册过的BroadcastReceiver,即使我们对应的程序已经关闭,但是只要我们的系统(比如说手机,平板)开启,这该BroadcastReceiver依然处于活动状态,依然可以接受到广播信息。

     如果我们需要实时监测某个广播信息,则可以采用在Mainfest.xml中进行注册,但如果我们用广播信息来更新activity的UI,则应该采用在java代码中进行注册。

 

DEMO1:

首先我们创建一个继承自BroadcastReceiver的接收者:

package com.example.broadcast;

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

public class TestReciver extends BroadcastReceiver {
 
    public TestReciver()
    {
        System.out.println("TestReceiver");
        
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("TestReciver --> OnReceiver");
    }

}

在配置文件中添加对应的节点:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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="com.example.broadcast.TestReciver">
            <intent-filter>
                <action android:name="android.intent.action.EDIT"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

在MainActivity.java中对广播的具体操作:

package com.example.broadcastdemo;

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

public class MainActivity extends Activity implements OnClickListener{

    private Button btnStart = null;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btnStart = (Button)findViewById(R.id.btnStart);
        btnStart.setOnClickListener(this);
      
    }

    @Override
    public void onClick(View view) {
        //采用intent发送数据
        Intent intent = new Intent();
        //只发送给action为ACTION_EDIT的对象
        intent.setAction(Intent.ACTION_EDIT);
        //通过广播方式发送该intent,只有特定action的接收者才能收到该广播信息
        MainActivity.this.sendBroadcast(intent);                                     
    }
}

 点击BUTTON按钮,测试结果:

技术分享

注意,只有匹配的Intent才会被执行,并不是所有的Receive都能接收到哦!

淘宝(阿里百川)手机客户端开发日记第六篇 广播机制详解(一)

标签:

原文地址:http://www.cnblogs.com/yushengbo/p/4614241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!