android入门——BroadCast(2)
自定义广播
<?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)的更多相关文章
- android入门——BroadCast(1)
使用广播要定义一个广播接收类,如 package com.example.wkp.broadcast; import android.content.BroadcastReceiver; import ...
- 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity
问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
- UniMelb Comp30022 IT Project (Capstone) - 1.Android入门
1. Android入门 Android系统架构 Android系统:四层架构.五块区域 1. Linux内核层 Linux Kernel:为Android设备的硬件提供了底层驱动 2. 系统运行库层 ...
- Android入门(十二)SQLite事务、升级数据库
原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...
- 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建
Xamarin.Android 入门之:Xamarin+vs2015 环境搭建 一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...
- android 入门 006(sqlite增删改查)
android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...
- android 入门 005(登录记住)
android 入门 005(登录记住) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Android入门:绑定本地服务
一.绑定服务介绍 前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...
- Android入门视频推荐
marschen老师的Android入门视频推荐网址: 1.Android应用程序开发视频教程(重制版)第一季 2.Android应用开发视频教程(重制版)第二季 2.marschen老师的个人微 ...
随机推荐
- hdu 1698 Just a Hook_线段树
题意:给你个n,表示区间[1,n],价值初始为1,给你m段区间和价值,更新区间,区间价值以最后更新为准,问更新后区间价值总和为多少 思路:两种方法,可以先存下来,倒过来更新,一更新节点马上跳出,比较快 ...
- windows Oracle DBases auto backUp
- c/c++性能优化--- cache优化的一点杂谈
之前写了一篇关于c/c++优化的一点建议,被各种拍砖和吐槽,有赞成的有反对的,还有中立的,网友对那篇博客的的评论和吐槽,我一个都没有删掉,包括一些具有攻击性的言论.笔者有幸阅读过IBM某个项目的框架代 ...
- Matlab lugui
function [L,U,pv,qv] = lugui(A,pivotstrat) %LUGUI Gaussian elimination demonstration. % % LUGUI(A) s ...
- gateone安装(web版本ssh)
前言: 好久都没来写博客,最近忙啥去了呢? 一是忙于saltstack的二次开发,二是云计算的学习研究中,所以就一直没写东西,今天给大家介绍个工具. 好了,开始正文! 1.首先来说一下为什么要web ...
- .net程序员转战android第二篇---牛刀小试
上篇说道如何搭建android的开发环境,这一篇我们将牛刀小试一下, 完成我们第一个android APP应用. 我就从新建项目说起吧. 首先打开Eclipse,选择顶部的File(文件)——new( ...
- Android onSaveInstanceState()
我们知道,由于手机的内存问题,很容易造成切换activity之后上一个activity被回收的情况,虽然我们按下back按键的时候,还是能够回到上一个activity,但是此时我们并不是执行的onRe ...
- Android Permissions管理之用户拒绝授权
Android Permissions管理之用户拒绝授权,在Marshmallow之前的安卓版本,应用的权限只需要注册一下,应用就会获取到,在Marshmallow之后,为了安全,全新的权限模型出现, ...
- 阿里云的linux命令小结
/** ---------------- [ 华丽分割线 ] ------------------------ ### 121.40.120.167 操作 ### 1.启动 nginx cd /usr ...
- shopncv4 短信接口 提供商 中国短信网
前提是在后台开启手机注册功能:具体是在设置->账号同步->手机短信 里开启. 修改 siteroot\core\framework\libraries\sms.php 修改 sit ...