Android MTK预置音乐/图片/文件

1.把mp3文件预置到手机system/meida目录下
/device/mediatek/project/device.mk

PRODUCT_COPY_FILES += packages/apps/3rd-party/mp3/ring35.mp3:system/media/ring35.mp3

2.创建service把system/media目录下的文件copy到sdcard

package com.android.providers.media;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

/**
 * mh.20180425
 */
public class CopyService extends Service {
    public CopyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try{
            File file = new File(*"/system/media/ring35.mp3"*);
            if(file.exists()){
                InputStream inStream = new FileInputStream(*"/system/media/ring35.mp3"*); 
                FileOutputStream fs = new FileOutputStream(*"/storage/sdcard0/Music/ring35.mp3"*);   
                byte[] buffer = new byte[1024];   
                int byteread;
                while ( (byteread = inStream.read(buffer)) != -1) {   
                    int bytesum = byteread; 
                    System.out.println(bytesum);   
                    fs.write(buffer, 0, byteread);   
                }   
                inStream.close();   
                fs.close();
            }   
            }catch(Exception e){
                e.printStackTrace();
            }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

3.监听开机广播,判断是否第一次开机并开启service
packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java

MediaProvider里有现成的广播接收或者在Settings或其他系统应用里监听均可

            sharedPreferences = context.getSharedPreferences("first_boot", Context.MODE_PRIVATE);
            boolean first_boot= sharedPreferences.getBoolean("first_boot", true);
            if (first_boot) {
               Intent activityIntent = new Intent();
               activityIntent.setClassName("com.android.providers.media", "com.android.providers.media.CopyService");
               context.startService(activityIntent);
            }
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("first_boot", false);
            editor.commit();

还有注册Service

        <service
            android:name="com.android.providers.media.CopyService"
            android:enabled="true"
            android:exported="true" >
        service>

你可能感兴趣的:(android客制)