返回顶部

收藏

动态注册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-30 19:55:36移动图片 by 廖钊权
  3. 2014-07-24 11:46:58监听文件夹 by 法名空虚
  4. 2014-09-01 14:01:01数据库操作 by sdcool
  5. 2014-09-22 21:31:40游戏里音乐初始化,与播放 by Koon.LY
  6. 2014-10-26 20:40:45Android 获取 IP 地址 by Hugh
  7. 2014-12-16 09:22:19java获得真实IP代码 by 金背二郎
  8. 2014-05-28 16:31:57按行获取TextView的内容 by sdcool
  9. 2014-07-15 11:19:06屏幕透明度 by liuyan814
  10. 2014-07-19 21:37:06启动Receiver by demon
  11. 2019-02-14 18:14:32黑客帝国全系列2070tv by 小狗3387

发表评论