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

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都能接收到哦!

时间: 2024-08-24 00:11:51

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

淘宝(阿里百川)手机客户端开发日记第四篇 自定义ListView详解

我们知道,如果采用官方的ListView,实现的功能在很多时候,并不能满足自己的业务需求,比如在设计到复杂的列表的时候,这一节,我们就开始动手自己实现自定义的ListView. 在上一节中,我们采用了固定的ListView中的数据集,我们对其进行改造:我们重新设置一个ListView的一个模板 template_sort.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a

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

Service和Thread的关系 不少初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread? 答案是Service和Thread之间没有任何关系! Service 是在后台运行的,但是它运行在主线程中,Service无法直接和UI进行交互,我们只有通过接口回调和广播机制(下一节将介绍广播机制)来实现对UI的操作: Thread 是用于开启一个子线程,执行一些耗时操作不会阻塞主线程的运行: 举个例子: 某些应用程序可

淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(五)

我们现在对上一节中的DEMO进行改进,在服务中开启线程来执行. package com.example.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.widget.Button; public class MyService

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

public abstract class Service; [API文档关于Service类的介绍] A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other appli

淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(二)

DEMO1:当我们点击启动服务时和点击停止服务的时候,观察服务的运行状态,布局由于简单,只是两个普通的Button按钮,在此我只上截图. java代码部分 第一步:我们需要实现一个服务类,继承自service,并实现其中的一些方法:这里我在每个方法里,打印出调用方法的名称. package com.example.service; import android.app.Service; import android.content.Intent; import android.os.IBinde

淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(四)

DEMO1:在Activity里声明一个回调方法,当service完成任务后,调用这个回调方法. 首先,我们先继承service,来创建服务,代码如下: 1 package com.example.service; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 import android.uti

淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(三)

主题:Service与Activity交互通信 问题的引出:现在有个需求,如果我们有一个下载任务,下载时间耗时比较长,并且当下载完毕后,需要更新UI的内容,这时,service中的bindService就派上用场了.读者可能到此头脑里有些疑问,Thread和Service都是在后台运行啊,为什么不用Thread呢?大家别急啊,这个我将在下节详细帮你分析下Service后台运行和Thread的运行之间的不同. 我们需要知道,service在运行时,它是和UI无关联的,它是在非UI线程里运行的.想让

淘宝(阿里百川)手机客户端开发日记第十篇 阿里百川服务器环境介绍

阿里百川的官方网址:http://baichuan.taobao.com/ ,具体详情,自己点击去查看. 提前声明:本人和淘宝一点关系没有,最近是自己有个项目,需要使用它们的平台,来开发android,IOS的客户端产品,所以将平时在实践过程中,很多重要的心得记下,方便自己后期遇到问题,随时查阅:同时,也能帮助很多爱好编程者提供一些资料,毫无其它目的,谢谢! 这节课,将向你介绍下它的大致内容:刚开始接触阿里百川,很多人会遇到各种各样的,貌似千奇百怪的问题,我将以简明的方式,让大家快速搭配环境,来

淘宝(阿里百川)手机客户端开发日记第十篇 什么是淘宝客(七)

在淘宝客中,有淘宝联盟.卖家.淘客以及买家四个角色.他们每个都是不可缺失的一环,如图所示. (1)淘宝联盟(http://www.alimama.com/):一个推广平台,帮助卖家推广产品:帮助淘客赚取利润,每笔推广的交易抽取相应的服务费用. (2)卖家:佣金支出者,他们提供自己需要推广的商品到淘宝联盟,并设置每卖出一个产品愿意支付的佣金. (3)淘宝客:佣金赚取者,他们在淘宝联盟中找到卖家发布的产品,并且推广出去,当有买家通过自己的推广链接成交后,那么就能够赚到卖家所提供的佣金(其中一部分需要