返回顶部

收藏

动态注册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. 铁木箱子 发表 2011-11-27 14:31:09 WAC启动Android模拟器时出现“transfer error: Read-only file
  2. Ray 发表 2015-05-19 07:55:08 Java操作图数据库Titan
  3. dulong 发表 2018-09-04 12:39:56 原 荐 Spring事务隔离级别详解
  4. bobshi 发表 2014-10-27 05:43:38 IDEA生成自动编译打包ant脚本
  5. darida 发表 2018-09-06 10:03:16 Apache POI 4.0.0 正式发布,不再支持 Java 6 与 7
  6. fyc198610 发表 2014-01-10 03:56:51 mac eclipse svn 文件名乱码
  7. xiaoli.he 发表 2018-09-09 08:46:10 Linux下Tomcat开启查看GC日志
  8. 博主 发表 2015-02-25 15:49:38 Mac安装Android开发环境
  9. Harries 发表 2018-09-11 12:39:58 Java随机数探秘
  10. ChenShan 发表 2015-04-08 16:00:00 install android sdk in macOS
  11. likai 发表 2018-09-13 12:25:58 Spring boot的缓存使用
  12. techug 发表 2015-06-04 04:08:18 Java程序员必须知道的10款免费IDE编辑器

发表评论