package com.jinhoward.broadcast.activity;

import com.jinhoward.broadcast.activity.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity
{
protected static final String MYACTION = "com.jinhoward.broadcast.ACTION";
private Button btnBroadcast;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnBroadcast=(Button)findViewById(R.id.btnBroadcast); btnBroadcast.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent=new Intent().setAction(MYACTION);
Log.i("MyReceiver", "按钮被点击");
sendBroadcast(intent);
}
});
}
}

receiver类:

package com.jinhoward.broadcast.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log; /**
* @author jinhoward
* @blog http://blog.csdn.net/jinhwoard
*/
public class MyReceiver extends BroadcastReceiver
{ private static final String TAG = "MyReceiver"; public MyReceiver()
{
Log.i(TAG, "MyReceiver的构造函数执行");
} @Override
public void onReceive(Context context, Intent intent)
{
Log.i(TAG, "接受到广播:"+intent.getAction());
} }

manifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jinhoward.broadcast.activity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name="com.jinhoward.broadcast.activity.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.jinhoward.broadcast.receiver.MyReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="com.jinhoward.broadcast.ACTION" />
</intent-filter>
</receiver>
</application> <uses-permission android:name="android.permission.RECEIVE_SMS" /> </manifest>

学习到知识:

1、非系统类的广播需要自己定义action并且自己发送广播。

2、系统自带的广播不需要定义action,也不需要发送广播,只需要注册相应的系统广播事件,进行处理即可。

3、broadcastreceiver的生命周期

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

android中broadcastreceiver的用法-manifest中注册。的更多相关文章

  1. android中broadcastreceiver的用法-代码中注册

    界面如下:     问题1:点击“解绑广播接收器“后再次点击”解绑广播接收器“后,程序崩溃,log信息如下: 08-04 05:04:35.420: E/AndroidRuntime(5521): F ...

  2. Java中的Socket用法

    转发链接:https://www.cnblogs.com/zhanglei93/p/6217384.html (1)Java中的Socket用法 Java中的Socket分为普通的Socket和Nio ...

  3. Thinkphp中distinct的用法

    Thinkphp中distincat的用法 TP中distinct()的用处主要是去除重复的值 在Thinkphp手册中也详细说明了(链接:http://document.thinkphp.cn/ma ...

  4. WPF中StringFormat的用法

    原文:WPF中StringFormat的用法 WPF中StringFormat的用法可以参照C#中string.Format的用法 1. C#中用法: 格式化货币(跟系统的环境有关,中文系统默认格式化 ...

  5. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  6. Android中BroadcastReceiver广播

    BroadCastReceiver 简介 广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadca ...

  7. Android中BroadcastReceiver组件具体解释

    Android系统的4个组件最终还剩一种组件了BroadcastReceiver,这个组件是全局监听器,能够监听系统全局的广播消息,能够方便的实现系统中不同组件之间的通信 BroadcastRecei ...

  8. android 中uri.parse()用法

    android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...

  9. Android Manifest 中 uses-feature 和 uses-permission的作用 关系和区别

    Manifest中的 <uses-permission android:name="android.permission.CAMERA" /> 和 <uses-f ...

随机推荐

  1. dpkg-query

    1.功能作用 查看软件包信息 2.位置 /usr/bin 3.格式用法 dpkg-query [<选项> ...] <命令> 4.主要参数 Commands: -s|--sta ...

  2. Mybatis学习——一对多关联表查询

    1.实体类 public class Student { private int id; private String name; } public class Classes { private i ...

  3. handler.post 为什么要将thread对象post到handler中执行呢?

    转载网址:http://www.cnblogs.com/crazypebble/archive/2011/03/23/1991829.html在Android中使用Handler和Thread线程执行 ...

  4. C#中的lock关键字

    前几天与同事激烈讨论了一下,有一点收获,记录起来. 首先给出MSDN的定义: lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.这是通过在代码块运行期间为给定对象获取互斥锁来实现的. ...

  5. Java中的10颗语法糖

    语法糖(Syntactic Sugar):也称糖衣语法,指在计算机语言中添加的某种语法,这种语法对语言的功能没有影响,但是更方便程序员使用.通常来说,使用语法糖能够增加程序的可读性,减少程序代码出错的 ...

  6. 《Python 学习手册4th》 第十一章 赋值、表达式和打印

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  7. Linux(CentOs)下安装Phantomjs + Casperjs

    Linux(CentOs)下安装Phantomjs + Casperjs 是参照cnMiss's Blog http://ju.outofmemory.cn/entry/70691的博客进行安装的 1 ...

  8. hadoop1.2.1 伪分布式配置

    主要配置 core-site.xml hdfs-site.xml mapred-site.xml

  9. 对Struts的理解

    1.struts是一个按MVC模式设计的Web层框架,其实他就是一个大大的servlet,这个Servlet名为ActionServlet,或是ActionServlet的子类.我们可以在web.xm ...

  10. libsvm使用方法总结

    1.所需要软件下载: (1)libsvm(http://www.csie.ntu.edu.tw/~cjlin/libsvm/) (2)python (3)gnuplot 画图软件(ftp://ftp. ...