自定义广播

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wkp.broadcast"> <!-- 声明自定义权力 -->
<permission android:name="com.example.wkp.broadcast.MY_PEMISSION"/>
<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">
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />--> <!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
</activity> <activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <receiver android:name=".MyReceiver2">
<intent-filter>
<action android:name="com.example.wkp.broadcast.MY_ACTION"/>
</intent-filter>
</receiver>
<!--静态注册 -->
<receiver android:name=".MyReceiver">
<intent-filter>
<!-- 动作 打开飞行模式时触发广播 -->
<!--<action android:name="android.intent.action.AIRPLANE_MODE"/>-->
<!-- 收到短信时 暂时无法实现 -->
<!--<action android:name="android.provider.Telephony.SMS_RECEIVED"/>-->
<!--<action android:name="android.provider.Telephony.SMS_DELIVER"/>-->
</intent-filter>
</receiver>
</application> <!-- 开启短信权限 -->
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission> <!-- 自定义权力 -->
<uses-permission android:name="com.example.wkp.broadcast.MY_PEMISSION"></uses-permission> </manifest>

AndroidMainfest.xml

注意声明自定义权力

<permission android:name="com.example.wkp.broadcast.MY_PEMISSION"/>

开启权力

<uses-permission android:name="com.example.wkp.broadcast.MY_PEMISSION"></uses-permission>

静态注册

<receiver android:name=".MyReceiver2">
<intent-filter>
<action android:name="com.example.wkp.broadcast.MY_ACTION"/>
</intent-filter>
</receiver>
package com.example.wkp.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log; /**
* Created by wkp on 2016/9/21.
*/
public class MyReceiver2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.v("hh","get broadcast");
}
}

MyReceiver2.java

接收器

package com.example.wkp.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button; /**
* Created by wkp on 2016/9/21.
*/
public class SecondActivity extends Activity {
private Button btn=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn= (Button) findViewById(R.id.send);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction("com.example.wkp.broadcast.MY_ACTION");
sendBroadcast(intent,"com.example.wkp.broadcast.MY_PEMISSION");
Log.v("hehe","already send");
}
});
}
}

SecondActivity.java

点击按钮发送广播

有序广播先定义先接收

abortBroadcast();忽略广播

可以通过android:priority制定优先级  不写为0

可以设置是否接收其他app的广播

可以设置是否被其他app接收广播 本地广播LocalBroadcastManager

private LocalBroadcastManager manager= LocalBroadcastManager.getInstance(this);

LocalReceiver receiver=new LocalReceiver();

IntentFilter filter=new IntentFilter();

filter.addAction("com.example.wkp.broadcast.MY_ACTION");

manager.registerReceiver(receiver,filter);

android入门——BroadCast(2)的更多相关文章

  1. android入门——BroadCast(1)

    使用广播要定义一个广播接收类,如 package com.example.wkp.broadcast; import android.content.BroadcastReceiver; import ...

  2. 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity

    问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...

  3. UniMelb Comp30022 IT Project (Capstone) - 1.Android入门

    1. Android入门 Android系统架构 Android系统:四层架构.五块区域 1. Linux内核层 Linux Kernel:为Android设备的硬件提供了底层驱动 2. 系统运行库层 ...

  4. Android入门(十二)SQLite事务、升级数据库

    原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...

  5. 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建

    Xamarin.Android 入门之:Xamarin+vs2015 环境搭建   一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...

  6. android 入门 006(sqlite增删改查)

    android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...

  7. android 入门 005(登录记住)

    android 入门 005(登录记住) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  8. Android入门:绑定本地服务

    一.绑定服务介绍   前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...

  9. Android入门视频推荐

      marschen老师的Android入门视频推荐网址: 1.Android应用程序开发视频教程(重制版)第一季 2.Android应用开发视频教程(重制版)第二季 2.marschen老师的个人微 ...

随机推荐

  1. alternaiate terms

    操作系统就是能让您的计算机工作 的一系列基本程序和实用工具; 大多数的软件至少都要卖几百块钱,您们怎么愿意白白把它送给别人? 您真正应该问的问题是软件公司怎么可以要您花那么多钱买他们的软件.写软件和制 ...

  2. c++崩溃错误2

    使用了内联函数: 在头文件中声明和定义内联函数是正确的 但是在头文件中声明内联函数,而在.cpp文件中定义了内联函数会导致崩溃的 .h class stu{ inline void str(): } ...

  3. C# 后台调用script使用类

    在网站的开发的时候,总是会用到一些前台的提示的script的代码,从项目中整理了一份常用的方法. public class Jscript { public Jscript() { // // TOD ...

  4. 初始Knockout

    Kncokout是一个轻量级的ui类库,通过应用MVVN模式得JavaScript前端简单化. MVVN模式:http://www.cnblogs.com/xueduanyang/p/3601471. ...

  5. 蜗牛爱课 -- iOS 设计模式之模板模式

    1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...

  6. Oracle 添加 scott 示例用户

    学习SQL有一段时间了,但是也忘记的差不多了,今天有赶紧复习复习,然后发现一个问题,为啥之前看的视频教程,马士兵用的Oracle有scott用户和那些表格,而我的没有?难道是Oracle取消了?然后百 ...

  7. hdu 4707 Pet hdu 2013 Asia Regional Online —— Warmup

    一道简单的搜索题目,建一个树,根节点是 0 ,连接的两个节点的距离是 1 ,求 到 根节点长度是2的节点的个数. #include<stdio.h> #include<string. ...

  8. bootstrap 3 のcheckbox-inline

         <div class="form-group">      <p class="control-label"><b> ...

  9. 安装ecshop出错

    在安装Ecshop的时候,遇到两个问题: 1.Strict Standards: Non-static method cls_image::gd_version() should not be cal ...

  10. 用python开发简单ftp程序

    根据alex老师视频开发的简单ftp程序,只能实现简单的get功能 ftp客户端程序: #!/usr/bin/env python #_*_ coding:utf-8 _*_ import socke ...