返回顶部

收藏

动态注册BroadcastReceiver

更多
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();    
            filter.addAction(action);        
            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");
            }

        }    

    }    
}
//该片段来自于http://outofmemory.cn

标签:java,手机开发

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2012-11-05 21:59:42java获得随机数代码 by 怪兽狂殴奥特曼
  2. 2014-05-31 20:18:35java加密类 by 落叶随风
  3. 2014-06-01 19:52:45android-电话录音 by demon
  4. 2014-08-22 20:49:20Android Notication事件 by sdcool
  5. 2014-09-12 11:44:02Android 平台创建 XY 图表的完整例子 by clt
  6. 2014-10-01 13:35:45android 程序崩溃 后台handle处理类 by 廖钊权
  7. 2014-05-31 20:36:45修改TabHost导航高度和背景颜色 by 香格里拉登
  8. 2014-06-05 20:43:56Android 编程设置 APN by liuyan814
  9. 2014-11-26 09:55:57捕获应用中的日志 by sdcool
  10. 2014-05-29 13:21:32实现android的AES加密技术 by clt
  11. 2014-05-31 13:46:16调用外部App by Hugh
相关聚客文章
  1. 铁木箱子 发表 2011-11-27 14:31:09 WAC启动Android模拟器时出现“transfer error: Read-only file
  2. 张开涛 发表 2015-08-03 14:23:13 我是如何构建一个持续发展的项目
  3. WeYo 发表 2015-04-13 16:00:00 Yaml for Java
  4. sunny 发表 2015-07-15 02:01:03 MAC系统下JTest死机问题的解决方法
  5. 博主 发表 2015-08-12 07:57:10 在命令行中使用Eclipse MAT工具
  6. 博主 发表 2011-10-14 04:11:31 用YAML构建数据测试DAO层
  7. 徐迁征 发表 2015-06-15 23:38:55 Java集合之 HashMap 核心源码解读
  8. 祖强 发表 2015-08-18 14:12:16 Java集合-List
  9. linux@linux.cn (linu 发表 2015-08-21 01:16:23 Hibernate ORM 5.0 正式发布,支持 Java 8
  10. 博主 发表 2015-01-29 15:29:48 Java版PHP的魔术方法__call
  11. itxuye 发表 2015-08-28 07:11:08 Effective Java 1-避免创建不必要的对象
  12. unvpn.com 发表 2015-08-28 16:25:53 Java编程入门——前言

发表评论