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老师的个人微 ...
随机推荐
- java MongoDB driver error infos
DataTables warning: table id=dateTable - Ajax error. For more information about this error, please s ...
- Java面试题之九
四十六.Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 对于这个题,只要弄清楚Math提供的三个与取整相关的方法就OK了. 1.ceil,英文含义是天花板,该 ...
- jqery ajax读取json文件
json文件数据 [ {"name":"哈哈··","email":"邮箱01","gender": ...
- json输出用法+jquery validate
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- android系统将普通应用升级为系统应用
作为一名程序员,有的时候并不是使用软件,而是去改造软件,不仅仅只是会编程而已,还要满足客户的需求.这样,才能开发出符合客户需求的应用,在关于到涉及到android底层的应用的时候,手机就需要root了 ...
- sql生成20位数随机数
declare @rnd nvarchar(50)set @rnd =''while LEN(@rnd)<20 begin set @rnd =@rnd + REPLACE ( CONVERT( ...
- 利用SQL语句产生分组序号
partition by关键字是分析性函数的一部分,它和聚合函数不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录,partition by用于给结果集分组,如果没 ...
- 27 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- *循环-01. 求整数段和【help】
/* * Main.c * 循环-01. 求整数段和 * Created on: 2014年6月18日 * Author: Boomkeeper ***测试木有通过**** */ #include & ...
- jcSQL词法分析器对字符串token的解析
上星期写完词法分析器的时候,曾遇上一个无关紧要却X疼的问题.毕竟是第一次完整地写整个语言的编译器(暂且这么叫着吧,解释器更靠谱),由于经验不足,在字符串解析这一块驻足了两天才解决掉,这里记录下来供以后 ...