Android客制化------开机拷贝文件到内置存储
16lz
2021-01-23
此方法尽量不要拷贝大文件
在Service中进行拷贝动作
package com.xxx.filecopy;import android.app.Service;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.AsyncTask;import android.os.IBinder;import java.io.File;public class CopyService extends Service { private final Context mContext; private String srcPath = "/system/extMedia/"; private String dstPath = "/mnt/sdcard/"; public CopyService() { mContext = CopyService.this; } @Override public void onCreate() { super.onCreate(); new AsyncTask() { @Override protected Integer doInBackground(Integer... params) { // TODO Auto-generated method stub Utils.log("onStart()"); SharedPreferences preference = (CopyService.this).getSharedPreferences("com.xxx.filecopy", MODE_PRIVATE); boolean alreadyRun = preference.getBoolean("runed", false); if(!alreadyRun){ File sdcard = new File("/mnt/sdcard/"); while(true){ try{ Thread.sleep(2000); }catch(Exception e){ Utils.log("sd Can't write, wait 2 secounds"); } // 判断sd卡是否为空且是否可写 if(null != sdcard && sdcard.canWrite()) break; } boolean b = Utils.copyDirectory(srcPath, dstPath); if(b){ Utils.log("Copy successful !"); Utils.scanFileAsync(mContext,dstPath); SharedPreferences.Editor editor = preference.edit(); editor.putBoolean("runed", true); editor.commit(); //播放视频 Utils.playmp4(mContext); } } mContext.stopService(new Intent(mContext, CopyService.class)); return null; } }.execute(0); } @Override public IBinder onBind(Intent intent) { return null; }}
第一次开机之后启动Service
package com.xxx.filecopy;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.view.Gravity;import static android.content.Context.MODE_PRIVATE;public class Receiver extends BroadcastReceiver { private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; private static final String DONT_TOUCH = "xxx.xxx.donttouch"; private Context mContext; @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. String action = intent.getAction(); mContext = context; Utils.log("action *****" + action); SharedPreferences preference = context.getSharedPreferences("com.xxx.filecopy", MODE_PRIVATE); boolean alreadyRun = preference.getBoolean("runed", false); if (action.equals(BOOT_COMPLETED)) { Intent i = new Intent(); i.setClass(context, CopyService.class); i.setFlags(i.FLAG_ACTIVITY_NEW_TASK); context.startService(i); if (alreadyRun){ Utils.log("RE_BOOT_COMPLETED,SO NEED PLAY"); Utils.playmp4(mContext); } }else if (action.equals(DONT_TOUCH)) { if (!Utils.isMusicActive(mContext)) { Utils.log("Media will play"); Utils.playmp4(mContext); } } String language = locale.getLanguage(); if (language.endsWith("ar")) { mNowContent.setGravity(Gravity.RIGHT| Gravity.TOP); } }}
工具类
package com.xxx.filecopy;import android.content.Context;import android.content.Intent;import android.media.AudioManager;import android.net.Uri;import android.util.Log;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;/** * Created by xxx on 2017/10/16. */public class Utils { /*** * 复制文件 * @param src 源文件 * @param des 目标文件 * @return */ private static boolean copyFile(String src, String des) { FileInputStream FIS = null; FileOutputStream FOS = null; File df = new File(des); if(df.exists() && df.length() == new File(src).length()){ return true; } try { FIS = new FileInputStream(src); FOS = new FileOutputStream(des); byte[] bt = new byte[1024]; int readNum = 0; while ((readNum = FIS.read(bt)) != -1) { FOS.write(bt, 0, readNum); } return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { if (FIS!=null) { try { FIS.close(); } catch (IOException e) { e.printStackTrace(); } } if (FOS!=null) { try { FOS.close(); } catch (IOException e) { e.printStackTrace(); } } } } /*** * 播放视频 * @param context */ public static void playmp4(Context context){ Uri parse = Uri.parse("file:///mnt/sdcard/video_demo.mp4"); Intent play_intent = new Intent(Intent.ACTION_VIEW); play_intent.setDataAndType(parse,"video/mp4"); play_intent.setFlags(FLAG_ACTIVITY_NEW_TASK); context.startActivity(play_intent); } /*** * 拷贝目录 * @param SrcDirectoryPath * @param DesDirectoryPath * @return */ public static boolean copyDirectory(String SrcDirectoryPath, String DesDirectoryPath) { log("copyDirectory()" + SrcDirectoryPath + " > " + DesDirectoryPath); try { File F0 = new File(DesDirectoryPath); if (!F0.exists()) { if (!F0.mkdir()) { log("create folder faild"); } } File F = new File(SrcDirectoryPath); File[] allFile = F.listFiles(); int totalNum = allFile.length; String srcName = ""; String desName = ""; int currentFile = 0; for (currentFile = 0; currentFile < totalNum; currentFile++) { log("start copy > " + allFile[currentFile].getAbsolutePath()); if (!allFile[currentFile].isDirectory()) { // 如果是文件是采用處理文件的方式 srcName = allFile[currentFile].toString(); desName = DesDirectoryPath + "/" + allFile[currentFile].getName(); copyFile(srcName, desName); } else { if (copyDirectory( allFile[currentFile].getPath().toString(), DesDirectoryPath + "/" + allFile[currentFile].getName().toString())) { } else { //log("SubDirectory Copy Error!"); } } //log("end copy > " + allFile[currentFile].getAbsolutePath()); } return true; } catch (Exception e) { //log(e.getMessage()); log("SubDirectory Copy Error!"); e.printStackTrace(); return false; } } /*** * 打印log * @param msg */ public static void log(String msg){ android.util.Log.d("xxx", "ALog > " + msg); } /*** * 扫面sd卡 * @param ctx * @param filePath */ public static void scanFileAsync(Context ctx, String filePath) { Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); scanIntent.setData(Uri.fromFile(new File(filePath))); ctx.sendBroadcast(scanIntent); } /*** * 判断当前系统时候在播放媒体资源 * @param context * @return */ public static boolean isMusicActive(Context context) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); if (audioManager == null) { log("isMusicActive(): couldn't get AudioManager reference"); return false; } return audioManager.isMusicActive(); }}
更多相关文章
- Android拓展系列(9)--Android视频录制screenrecord命令
- android利用videoView播放视频(已封装)
- android log日志文件输出
- android按行读取文件内容的几个方法
- android 文件 修改权限
- android 打开不同文件工具类
- 使用命令行ls命令的Android文件浏览控件,适合于root过的设备,可以
- Android日志服务例子,将日志保存在文件中并每天产生一个日志文件