Android6.0 MediaScanner多媒体文件开机扫描及修改默认铃声(一)

本文深入剖析Android系统开机时如何扫描内部和外部存储的多媒体文件并添加到数据库,以及如何修改默认铃声。重点讨论MediaScannerReceiver监听开机和SD卡挂载广播,MediaScannerService的启动以及MediaScanner类在文件扫描中的关键作用。
摘要由CSDN通过智能技术生成

这边文章主要来分析开机启动时候的多媒体文件扫描和如何修改系统默认铃声。

先来看开机启动时候的多多媒体文件扫描流程分析:

Android系统中,在开机的时候会去扫描内部存储器和外部存储器内的文件资源并将其添加到相应的数据库中,方便系统或者应用去引用相应的资源文件。

在android系统中,使用MediaScannerReceiver.java类来监听系统开机启动和SD卡挂载的广播,以此来执行相应的后续操作。

先来看MediaScannerReceiver.java在清单文件中的配置:

<receiver android:name="MediaScannerReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_MOUNTED" />
                <data android:scheme="file" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
                <data android:scheme="file" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_SCANNER_SCAN_FILE" />
                <data android:scheme="file" />
            </intent-filter>
        </receiver>
静态注册开机广播、SD卡挂载/卸载广播、文件扫描广播等等;

MediaScannerReceiver.java继承于BroadcastReceiver,先来看onReceive()方法;

    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        final Uri uri = intent.getData();
        if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
            // Scan both internal and external storage
            scan(context, MediaProvider.INTERNAL_VOLUME);
            scan(context, MediaProvider.EXTERNAL_VOLUME);

        } else {
            if (uri.getScheme().equals("file")) {
                // handle intents related to external storage
                String path = uri.getPath();
                String externalStoragePath = Environment.getExternalStorageDirectory().getPath();
                String legacyPath = Environment.getLegacyExternalStorageDirectory().getPath();

                try {
                    path = new File(path).getCanonicalPath();
                } catch (IOException e) {
                    Log.e(TAG, "couldn't canonicalize " + path);
                    return;
                }
                if (path.startsWith(legacyPath)) {
                    path = externalStoragePath + path.substring(legacyPath.length());
                }

                Log.d(TAG, "action: " + action + " path: " + path);
                if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
                    // scan whenever any volume is mounted
                    scan(context, MediaProvider.EXTERNAL_VOLUME);
                } else if (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action) &&
                        path != null && path.startsWith(externalStoragePath + "/")) {
                    scanFile(context, path);
                }
            }
        }
    }
Intent.ACTION_BOOT_COMPLETED事件:

                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值