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. poj 2230 Watchcow(欧拉回路)

    关键是每条边必须走两遍,重复建边即可,因为确定了必然存在 Euler Circuit ,所以所有判断条件都不需要了. 注意:我是2500ms跑过的,鉴于这道题ac的code奇短,速度奇快,考虑解法应该 ...

  2. [Sciter系列] MFC下的Sciter–5.Sciter中GUI线程研究

    [Sciter系列] MFC下的Sciter–5.Sciter中GUI线程研究,目前MFC存在问题,win32没问题. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的Sc ...

  3. vim 支持 markdown 语法

    /************************************************************************* * vim 支持 markdown 语法 * 说明 ...

  4. Control File (二)重建CONTROLFILE --- NORESETLOG

    create controlfile  --- noresetlog 由于丢失control01.ctl  alter_karl.log 中显示: -------------------------- ...

  5. 【转】从INF文件认识驱动

    在工控机安装xp操作系统时,由于工控机的集成显卡驱动只支持win7,之前没接触过windows驱动相关内容,折腾了半天.下载的驱动是exe的,双击安装就提示安装失败(未签名) 上图是网上随便找的,现象 ...

  6. [转] GIS二次开发(C#+AE)

    乘风莫邪 原文GIS二次开发(C#+AE) 此过程描述了使用ArcGIS控件建立和部署应用的方法和步骤. 你可以在下面的目录下找到相应的样例程序: <</FONT>安装目录>/ ...

  7. [Everyday Mathematics]20150126

    设 $A$ 是 $4\times 2$ 阶实矩阵, $B$ 是 $2\times 4$ 阶实矩阵, 满足 $$\bex AB=\sex{\ba{cccc} 1&0&-1&0\\ ...

  8. 通过ListActivity使用ListView布局方法

    先简单的介绍一下ListActivity ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来.ListA ...

  9. UI篇--Android中3种方法实现back键动作

    方法一:重写onBackPressed方法 @Override public void onBackPressed() { // do something what you want super.on ...

  10. LoadRunner学习记录--Flights打开空白页的问题

    从网上查了一下,原因是PERL5LIB这个环境变量的原因. 担心修改环境变量会影响ORACLE的运行 在WebTour中修改run.bat   增加 set PERL5LIB=D:\oracle\pr ...