当前位置: 首页 > news >正文

动态注册BroadcastReceiver

1. [代码][Java]代码     
package com.zjt.innerreceiver;
 
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
 
public class ServiceTest extends Service{
 
public final static String TAG = "ServiceTest";
 
private Receiver mReceiver;
 
@Override
public IBinder onBind(Intent arg0) {
return null;
}
 
@Override
public void onCreate() {
super.onCreate();
//注册Receiver
mReceiver = new Receiver(this);  
mReceiver.registerActionAndScheme(Intent.ACTION_MEDIA_EJECT, "file"); 
mReceiver.registerActionAndScheme(Intent.ACTION_MEDIA_REMOVED, "file"); 
mReceiver.registerActionAndScheme(Intent.ACTION_MEDIA_MOUNTED, "file"); 
mReceiver.registerActionAndScheme(Intent.ACTION_PACKAGE_REMOVED, "package"); 
                mReceiver.registerActionAndScheme(Intent.ACTION_PACKAGE_ADDED, "package");
}
 
@Override
public void onDestroy() {
super.onDestroy();
//注销Receiver
unregisterReceiver(mReceiver);  
}
 
 
 
class Receiver extends BroadcastReceiver {    
        
Context mContext;    
  
        public Receiver(Context context){    
            mContext = context;    
        }    
            
        //动态注册  
        public void registerAction(String action){    
            IntentFilter filter = new IntentFilter();    
            filter.addAction(action);        
            mContext.registerReceiver(this, filter);    
        }  
        
        public void registerActionAndScheme(String action, String dataScheme){    
            IntentFilter filter = new IntentFilter();   http://www.huiyi8.com/css3/ 
            filter.addAction(action);       css3教程 
            filter.addDataScheme(dataScheme);
            mContext.registerReceiver(this, filter);    
        } 
            
        @Override    
        public void onReceive(Context context, Intent intent) {    
         String action = intent.getAction();
         Log.d(TAG, "action:" + action);
        
         if(action.equals(Intent.ACTION_MEDIA_EJECT)) {
         Log.d(TAG , "sdcard has been ejected");
         } else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {
         Log.d(TAG , "sdcard has been removed");
         } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
         Log.d(TAG , "sdcard has been mounted");
         } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
         Log.d(TAG, "Intent.ACTION_PACKAGE_REMOVED");
         } else if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
         Log.d(TAG, "Intent.ACTION_PACKAGE_ADDED");
         }
             
        }    
            
    }    
}

转载于:https://www.cnblogs.com/xkzy/p/3806091.html

相关文章:

  • ASP.NET怎么防止多次点击提交按钮重复提交
  • @html.ActionLink的几种参数格式
  • [ASP.NET MVC]如何定制Numeric属性/字段验证消息
  • HTML中Select的使用具体解释
  • 使用Spring整合javaMail发用邮件
  • v​n​c​服​务​​安​装​与配置
  • 布局文件提示错误“No orientation specified, and the default is horizontal. This is a common so...”...
  • BZOJ-2743 采花
  • vs2012 发布网站丢失文件
  • hdu 1576扩展欧几里得算法
  • WCF入门教程:WCF基础知识问与答(转)
  • 《海量数据库解决方式》读后感
  • Winsock网络编程笔记(1)----入门
  • php中body下出现莫名空白字符
  • 关于ios 运行时 介绍的比较详细的帖子
  • 03Go 类型总结
  • egg(89)--egg之redis的发布和订阅
  • EOS是什么
  • ES6 ...操作符
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • js中forEach回调同异步问题
  • JS字符串转数字方法总结
  • learning koa2.x
  • MySQL用户中的%到底包不包括localhost?
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • Python利用正则抓取网页内容保存到本地
  • storm drpc实例
  • swift基础之_对象 实例方法 对象方法。
  • 浮动相关
  • 关于Flux,Vuex,Redux的思考
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • scrapy中间件源码分析及常用中间件大全
  • Semaphore
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • #{} 和 ${}区别
  • #宝哥教你#查看jquery绑定的事件函数
  • #控制台大学课堂点名问题_课堂随机点名
  • $$$$GB2312-80区位编码表$$$$
  • (2015)JS ES6 必知的十个 特性
  • (二)PySpark3:SparkSQL编程
  • (剑指Offer)面试题34:丑数
  • (每日持续更新)jdk api之StringBufferInputStream基础、应用、实战
  • (三)docker:Dockerfile构建容器运行jar包
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • *Django中的Ajax 纯js的书写样式1
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .NET Framework杂记
  • .net refrector
  • .NET的数据绑定
  • [ 环境搭建篇 ] 安装 java 环境并配置环境变量(附 JDK1.8 安装包)
  • [ 数据结构 - C++] AVL树原理及实现
  • [ 云计算 | AWS ] AI 编程助手新势力 Amazon CodeWhisperer:优势功能及实用技巧