android之自定义广播
布局文件
点击按钮发送广播
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="发送广播"/>
</LinearLayout>
activity
在Activity中监听按钮事件并创建意图对象,通过sendBroadcast将意图对象发送出去
package xidian.dy.com.chujia; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity {
SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp = getSharedPreferences("hate", Context.MODE_PRIVATE);
Button button = (Button) findViewById(R.id.sure);
if(button != null)
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.dy.xd.test");
sendBroadcast(intent);
}
});
}
}
广播接收者
package xidian.dy.com.chujia; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; /**
* Created by dy on 2016/7/12.
*/
public class Custom extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(context, "我的广播" +action, Toast.LENGTH_SHORT).show();
}
}
清单文件
在清单文件中需要我们在创建意图对象时设置的action
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xidian.dy.com.chujia">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="主界面">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Custom">
<intent-filter>
<action android:name="com.dy.xd.test" />
</intent-filter>
</receiver>
</application>
</manifest>
android之自定义广播的更多相关文章
- android 发送自定义广播以及接收自定义广播
发送自定义广播程序: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- android: 发送自定义广播
5.3.1 发送标准广播 在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发 出去也是白发.因此新建一个 MyBroadcastReceiver 继承自 Broadca ...
- Android 发送自定义广播(标准和本地)
1.首先自定义一个广播接收器:MyBroadcastReceiver package example.com.mybroadcastreceiver; import android.content.B ...
- Android中自定义广播的实现
今天尝试了自定义的广播,说是自定义的广播其实质就是自己编写一个类继承broadcastreceiver类,然后再onreceiver方法中实现某些功能,在这个过程中我们可以自定义我们要发出的广播,可以 ...
- Android(java)学习笔记178:BroadcastReceiver之 自定义广播
广播使用: 电台:对外发送信号.---------电台发送广播(可以自定义) 收音机:接收电台的信号.-----广播接收者 这里,我们就说明自定 ...
- [android] 自定义广播事件
上一节的短信拦截在4.0以上系统中无效,可以使用这种办法实现,定义一个activity,清单文件中指定主题为透明,在onCreate()方法里面直接调用finsh()方法,关掉,这样可以就可以实现了 ...
- 自定义广播(BroadcastReceiver)事件 --Android开发
本例演示自定义广播事件.我们需要做的是,在主活动中写发送广播的代码,然后在接收广播的类中写接收广播的代码. 1.主活动中点击按钮后发送广播 MainActivity.java: public clas ...
- android中实现自定义广播
自定义广播分两个步骤:1.发送广播 2.接收广播 一.先看如何接收广播: 我使用的是Android Studio,File->New->Other->Broadcast Receiv ...
- MIUI7,Android版本5.0.2,一个程序发送自定义广播,另一个程序没有接收到
对照<第一行代码——Android>进行学习,第五章中说到广播包的相关知识,前面获取广播等程序例程都可以跑的通,但是在5.3.2节中,程序A发送自定义广播,并接收自定义广播,同时程序B也接 ...
随机推荐
- tools:context=".MainActivity的作用(转)
android:layout_width="match_parent" android:layout_height="match_parent" android ...
- Java 集合系列13之 WeakHashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对WeakHashMap进行学习.我们先对WeakHashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用WeakHashMap.第1部分 WeakHashMap介绍 ...
- Oracle Dataguard的原理与基本配置
最近集团在做灾备方案,用于Oracle的高可用性,在不影响主库性能的前提下,我们选择使用DG的"最大性能"模式. DG是Oracle数据库自带的数据同步功能,其基本原理是将日志 ...
- MMORPG大型游戏设计与开发(客户端架构 part16 of vegine)
由于近来比较忙碌和有些困倦的原因,所以关于这部分的文章没有及时更新,一句话:让朋友们久等了!今天所讲的是客户端vengine(微引擎)中最后一个部分,就像上节所说,这一部分的内容比较多.可能有些朋友看 ...
- Wish You to Remember
Just to myself: it is not complicate. And I don't know its internal principle now. (ms08-067) But I ...
- CSS Sprite雪碧图应用
在写网页过程中,会遇到这种需要使用多个小图标: 如上图中的「女装」文字左边的图标.容易想到的解决方法是为每张图片加入<img>标签,但这样做会增加HTTP请求数量,影响网站加载速度.比这更 ...
- Could not load file or assembly 'MySql.Data.CF,
Could not load file or assembly 'MySql.Data.CF, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c56 ...
- LESS速查
注释 缓存式注释/*注释内容*/ 非缓存式注释//注释内容 变量 @nice-blue: #5B83AD; @light-blue: @nice-blue + #111; #header { col ...
- Win7 + ubuntu14.04 双系统安装教程
安装主要分为以下几步: 一. 下载Ubuntu 14.04镜像软件: 二. 制作U盘启动盘: 三. 安装Ubuntu系统: 四. 用EasyBCD 创建启动系统. 1. 下载 Ubuntu 14.04 ...
- Windows 7 USB DVD Download Tool
1.下载.安装.运行Windows 7 USB DVD Download Tool: 百度官方下载 http://rj.baidu.com/soft/detail/20458.html 2.点击&qu ...