android 监听SDCard安装和卸载的代码详解编程语言

// 监听类 
private final BroadcastReceiver sdcardListener = new BroadcastReceiver() { 
 
        @Override 
        public void onReceive(Context context, Intent intent) { 
 
            String action = intent.getAction(); 
            Log.d("TAG", "sdcard action:::::" + action); 
            if(Intent.ACTION_MEDIA_MOUNTED.equals(action) 
                    || Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action) 
                    || Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action) 
                    ){ 
                // SD卡成功挂载 
 
            } else if(Intent.ACTION_MEDIA_REMOVED.equals(action) 
                    || Intent.ACTION_MEDIA_UNMOUNTED.equals(action) 
                    || Intent.ACTION_MEDIA_BAD_REMOVAL.equals(action) 
                    ){ 
                // SD卡挂载失败 
 
            } 
 
        } 
    }; 
 
        // 注册监听 
    private void registerSDCardListener(){ 
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL); 
        intentFilter.addDataScheme("file"); 
        registerReceiver(sdcardListener, intentFilter); 
    }

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10791.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论