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. Error: "DEVELOPER_DIR" is not defined at ./symbolicatecrash line 53

    项目问题解析“Error: "DEVELOPER_DIR" is not defined at ./symbolicatecrash line 53.”这个问题是最近调试app的时 ...

  2. RegExp类型和text()方法

    ECMAScript通过RegExp类型来支持正则表达式 RegExp 实例方法:text() 它接受一个字符串参数,在模式与该参数匹配的情况下返回true,否则返回false,通常用在if语句中 / ...

  3. sql server 2012序列号

    MICROSOFT SQL SERVER 2012 企业核心版激活码序列号: FH666-Y346V-7XFQ3-V69JM-RHW28 MICROSOFT SQL SERVER 2012 商业智能版 ...

  4. C#中常见的winform控件命名规范

    我们知道Button 常常简称为btn,那么Winform中的其它控件呢,这篇文章在C#的winform控件命名规范 的基础上对一些控件的名称的简称进行了整理. 1. 标准控件 NO. 控件类型简写 ...

  5. git学习一

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  6. IOS 时间 日历 处理集合

    1.获得当前时间 从1970开始的秒数 NSTimeInterval time = [[NSDate date[ timeIntervalSince1970]]; NSString * str = [ ...

  7. Backbone.js developer 武汉 年薪8w-10w

    1.   精通Backbone.js 2.   熟练Ajax.NoSQL.RESTful APIs 3.   了解Pusher.com和 Parse.com 4.   具有良好的沟通能力,学习能力,敬 ...

  8. 【暑假】[实用数据结构]范围最小值问题(RMQ)

    范围最小值问题: 提供操作: Query(L,R):计算min{AL ~ AR } Sparse-Table算法: 定义d[i][j]为从i开始长度为2j的一段元素的最小值.所以可以用递推的方法表示. ...

  9. Tip提示框另类写法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. mysql 查看锁表解锁

    -- 查看那些表锁到了show OPEN TABLES where In_use > 0;-- 查看进程号show processlist;--删除进程 kill 42236: