BroadcastReceiver的两种注册方式之------静态注册
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dip"
android:gravity="center"
android:text="BroadcastReceiver演示"/> <Button
android:id="@+id/send_static"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送自定义静态注册广播" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.broaccast"
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=".StaticReceiver">
<intent-filter>
<action android:name="com.my.staticreceiver" />
</intent-filter>
</receiver>
</application> </manifest>
MainActivity.java
package com.my.broaccast; 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; public class MainActivity extends Activity { private Button sendStaticButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); sendStaticButton = (Button) findViewById(R.id.send_static);
sendStaticButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.my.staticreceiver");
intent.putExtra("msg", "接收静态注册广播成功!");
sendBroadcast(intent);
}
});
}
}
StaticReceiver.java
package com.my.broaccast; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class StaticReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if ("com.my.staticreceiver".equalsIgnoreCase(intent.getAction())) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
}
BroadcastReceiver的两种注册方式之------静态注册的更多相关文章
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...
- 转 Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再废叙述废话. 序言 对于所谓的认证说到 ...
- 动态代理的两种实现方式(JDK/Cglib)
=========================================== 原文链接: 动态代理的两种实现方式(JDK/Cglib) 转载请注明出处! ================== ...
- Java中String对象两种赋值方式的区别
本文修改于:https://www.zhihu.com/question/29884421/answer/113785601 前言:在java中,String有两种赋值方式,第一种是通过“字面量”赋值 ...
- Nginx 和 PHP 的两种部署方式比较
2种部署方式简介 第一种 前置1台nginx服务器做HTTP反向代理和负载均衡 后面多态服务器部署Nginx Web服务和php-fpm提供的fast cgi服务 第二种 前置1台nginx服务器做W ...
- Android四大组件之服务的两种启动方式详解
Service简单概述 Service(服务):是一个没有用户界面.可以在后台长期运行且可以执行操作的应用组件.服务可由其他应用组件启动(如:Activity.另一个service).此外,组件可以绑 ...
- JAVA高级架构师基础功:Spring中AOP的两种代理方式:动态代理和CGLIB详解
在spring框架中使用了两种代理方式: 1.JDK自带的动态代理. 2.Spring框架自己提供的CGLIB的方式. 这两种也是Spring框架核心AOP的基础. 在详细讲解上述提到的动态代理和CG ...
- Java连载66-数组的两种初始化方式
一.数组 1.数组中存储元素的类型是统一的,每一个元素在内存中所占用的空间大小是相同的,知道数组的首元素的内存地址,要查找的元素只要知道下标,就可以快速的计算出偏移量,通过首元素内存地址加上偏移量,就 ...
随机推荐
- iOStextView的代理方法展示
UITextView的代理方法 textViewShouldBeginEditing: and textViewDidBeginEditing: - (BOOL)textViewShouldBegin ...
- Debian安装autoconf
###安装autoconf wget http://mirrors.kernel.org/gnu/autoconf/autoconf-2.65.tar.gz \ && tar -xzv ...
- MC 自己平均
using System; using System.Drawing; using System.Linq; using System.Collections; namespace PowerLang ...
- MySQL性能优化的最佳21条经验【转载】
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...
- Inno Setup入门(二十二)——Inno Setup类参考(8)
: Install Setup 2013-02-02 11:31 477人阅读 评论(0) 收藏 举报 列表框 列表框(ListBox)是Windows应用程序中重要的输入手段,其中包括多个选项用户可 ...
- libprotobuff8.so not found
http://stackoverflow.com/questions/25518701/protobuf-cannot-find-shared-libraries
- HTML、XHTML和HTML5区别与联系
1.XHTML与HTML最大的区别: ① XHTML标签名必须小写(错误:<Div> 正确:<div>) ② XHTML元素必须被关闭(错误:<p> 正确:< ...
- java设计模式案例详解:观察者模式
观察者模式的应用场景: 1. 对一个对象状态的更新,需要其他对象同步更新,而且其他对象的数量动态可变. 2. 对象仅需要将自己的更新通知给其他对象而不需要知道其他对象的细节. 举个例子说明,这个例子讲 ...
- layer弹出层
最近因为项目要求做了一个layer弹出层demo,先看效果图 好了,现在开始上代码 index.jsp <%@ page language="java" import=&qu ...
- PHP商品倒计时 php实现当前用户在线人数
//PHP商品倒计时 date_default_timezone_set("Asia/shanghai");//地区 //配置每天的活动时间段 $starttimestr = &q ...