广播接收器(BroadcastReceiver)的使用

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;

import android.content.IntentFilter;
import android.net.Uri;
import android.os.storage.StorageVolume;


public class MountReceiver extends BroadcastReceiver {
    private static final String TAG="MountReceiver";

    private final MountPointManager mMountPointManager;
    private final ArrayList<MountListener>mMountListenerList=new ArrayList<MountListener>();
    private static final String INTENT_SD_SWAP="com.mediatek.SD_SWAP";

    public interface MountListener{
        /**
         * This method will be called to do things before MountPoint init.
         */
        //void prepareForMount(String mountPoint);

        /**
         * This method will be called when receive a mounted intent.
         */
        void onMounted(String mountPoint);

        /**
         * This method will be implemented by its class who implements this interface,
         * and called when receive a unMounted intent.
         */
        void onUnMounted(StorageVolume volume);

        /**
         * This method cancel the current action on the SD card which will be unmounted
         */
        void onEjected(String unMountPoint);

        /**
         * This method re-load volume info when sd swap on/off
         */
        void onSdSwap();

    }

    /**
     * This method gets MountPointManager's instance
     */
    public MountReceiver(){
        mMountPointManager=MountPointManager.getInstance();
    }

    /**
     * This method adds listener for activities
     *
     * @param listener listener of certain activity to respond mounted and unMounted intent
     */
    public void registerMountListener(MountListener listener){
        mMountListenerList.add(listener);
    }

    public void unregisterMountListener(MountListener listener){
        mMountListenerList.remove(listener);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String mountPoint = null;
        StorageVolume volume = null;
        Uri mountPointUri = intent.getData();
        if (mountPointUri != null) {
            mountPoint = mountPointUri.getPath();
        }
        LogUtils.d(TAG, "onReceive,handle SD_SWAP");
        if(INTENT_SD_SWAP.equals(action)){
            synchronized (this){
                for(MountListener listener:mMountListenerList){
                    LogUtils.d(TAG,"onReceive,handle SD_SWAP");
                    listener.onSdSwap();
                }
            }
        }

        if(mountPoint==null || mountPointUri==null){
            return;
        }

        if(Intent.ACTION_MEDIA_MOUNTED.equals(action)){
            //cancel the current operation before MountPointManager init
            //to avoid concurrently access to mountpoint arraylist.
            for(MountListener listener:mMountListenerList){
                listener.onMounted(mountPoint);
            }
        }else if(Intent.ACTION_MEDIA_UNMOUNTED.equals(action)){
            volume=(StorageVolume)intent.getExtras(StorageVolume.EXTRAZ_STORAGE_VOLUME);
            mMountPointManager.changeMountState(mountPoint,false);
            for(MountListener listener:mMountListenerList){
                listener.onUnMounted(volume);
            }
        }else if(Intent.ACTION_MEDIA_EJECT.equals(action)){
            for(MountListener listener:mMountListenerList){
                listener.onEjected(mountPoint);
            }
        }
    }

    /**
     * Register a MountReceiver for context.See
     * {@link Intent.ACTION_MEDIA_MOUNTED}{@link Intent.ACTION_MEDIA_UNMOUNTED}
     *
     * @param context Context to use
     * @return A mountReceiver
     */

    public static MountReceiver registerMountReceiver(Context context){
        MountReceiver receiver=new MountReceiver();

        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
        intentFilter.addDataScheme("file");
        context.registerReceiver(receiver,intentFilter);

        if(OptionsUtils.isMtkSDSwapSurpported()){
            IntentFilter intentFilterSDSwap=new IntentFilter();
            intentFilterSDSwap.addAction(INTENT_SD_SWAP);
            context.registerReceiver(receiver,intentFilterSDSwap);
        }
        return receiver;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值