• 动态注册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");
             }
                 
            }    
                
        }    
    }
  • 相关阅读:
    构建之法东北师大站分数构成
    alpha发布排序结果
    展示一个响应式布局吧,没有效果图自己敲敲看吧@@@@
    vc++和LINGO编程
    [opencv] 极线校正
    [python] 小demo
    [python] socket模块(TCP/IP网络编程)
    树莓派学习【二】:(SSH+VNC)树莓派一根网线连电脑,不要显示屏
    树莓派学习【一】:树莓派的SSH连接
    css+div基本知识;
  • 原文地址:https://www.cnblogs.com/xkzy/p/3806091.html
Copyright © 2020-2023  润新知