首页 文章 精选 留言 我的

精选列表

搜索[智能内容安全审核],共10000篇文章
优秀的个人博客,低调大师

Android--监测文件夹内容变化

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/77932391 一. 概述: android.os包下的FileObserver类是一个用于监听文件访问、创建、修改、删除、移动等操作的监听器,基于linux的INotify。FileObserver是个抽象 类,必须继承它才能使用。每个FileObserver对象监听一个单独的文件或者文件夹,如果监视的是一个文件夹,那么文件夹下所有的文件和级联子目录的改 变都会触发监听的事件。 二. 监听的事件类型: ACCESS,即文件被访问 MODIFY,文件被 修改 ATTRIB,文件属性被修改,如 chmod、chown、touch 等 CLOSE_WRITE,可写文件被 close CLOSE_NOWRITE,不可写文件被 close OPEN,文件被 open MOVED_FROM,文件被移走,如 mv MOVED_TO,文件被移来,如 mv、cp CREATE,创建新文件 DELETE,文件被删除,如 rm DELETE_SELF,自删除,即一个可执行文件在执行时删除自己 MOVE_SELF,自移动,即一个可执行文件在执行时移动自己 CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) ALL_EVENTS,包括上面的所有事件 三.实例 import com.example.androidemail.R; import com.example.androidemail.R.layout; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.os.FileObserver; public class AndroidFileListenerActivity extends Activity { private FileObserver mFileObserver; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(null == mFileObserver) { mFileObserver = new SDCardFileObserver(Environment.getExternalStorageDirectory().getPath()); mFileObserver.startWatching(); //开始监听 } } public void onDestory() { if(null != mFileObserver) mFileObserver.stopWatching(); //停止监听 } static class SDCardFileObserver extends FileObserver { //mask:指定要监听的事件类型,默认为FileObserver.ALL_EVENTS public SDCardFileObserver(String path, int mask) { super(path, mask); } public SDCardFileObserver(String path) { super(path); } @Override public void onEvent(int event, String path) { final int action = event & FileObserver.ALL_EVENTS; switch (action) { case FileObserver.ACCESS: System.out.println("event: 文件或目录被访问, path: " + path); break; case FileObserver.DELETE: System.out.println("event: 文件或目录被删除, path: " + path); break; case FileObserver.OPEN: System.out.println("event: 文件或目录被打开, path: " + path); break; case FileObserver.MODIFY: System.out.println("event: 文件或目录被修改, path: " + path); break; case FileObserver.CREATE: System.out.println("event: 文件或目录被创建, path: " + path); break; } } } } onEvent是回调,系统监听到事件后会触发此事件,参数event就是上面所说的事件类型,参数path就是触发事件的目录,鉴定只针对于该层目录,其他层次无效。 我们大多数需要监听path目录下的所有文件对象的相关操作,那该如何是好呢?解决问题方法之一就是重新实现FileObserver类, 下面是对FileObserver类的重写实现过程 import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Stack; import android.os.FileObserver; import android.util.Log; @SuppressWarnings(value = { "rawtypes", "unchecked" }) public class MultiFileObserver extends FileObserver { /** Only modification events */ public static int CHANGES_ONLY = CREATE | MODIFY |DELETE | CLOSE_WRITE | DELETE_SELF | MOVE_SELF | MOVED_FROM | MOVED_TO; private List<SingleFileObserver> mObservers; private String mPath; private int mMask; public MuityFileObserver(String path) { this(path, ALL_EVENTS); } public MuityFileObserver(String path, int mask) { super(path, mask); mPath = path; mMask = mask; } @Override public void startWatching() { if (mObservers != null) return; mObservers = new ArrayList<SingleFileObserver>(); Stack<String> stack = new Stack<String>(); stack.push(mPath); while (!stack.isEmpty()) { String parent = stack.pop(); mObservers.add(new SingleFileObserver(parent, mMask)); File path = new File(parent); File[] files = path.listFiles(); if (null == files) continue; for (File f : files) { if (f.isDirectory() && !f.getName().equals(".") && !f.getName().equals("..")) { stack.push(f.getPath()); } } } for (int i = 0; i < mObservers.size(); i++) { SingleFileObserver sfo = mObservers.get(i); sfo.startWatching(); } }; @Override public void stopWatching() { if (mObservers == null) return; for (int i = 0; i < mObservers.size(); i++) { SingleFileObserver sfo = mObservers.get(i); sfo.stopWatching(); } mObservers.clear(); mObservers = null; }; @Override public void onEvent(int event, String path) { switch (event) { case FileObserver.ACCESS: Log.i("RecursiveFileObserver", "ACCESS: " + path); break; case FileObserver.ATTRIB: Log.i("RecursiveFileObserver", "ATTRIB: " + path); break; case FileObserver.CLOSE_NOWRITE: Log.i("RecursiveFileObserver", "CLOSE_NOWRITE: " + path); break; case FileObserver.CLOSE_WRITE: Log.i("RecursiveFileObserver", "CLOSE_WRITE: " + path); break; case FileObserver.CREATE: Log.i("RecursiveFileObserver", "CREATE: " + path); break; case FileObserver.DELETE: Log.i("RecursiveFileObserver", "DELETE: " + path); break; case FileObserver.DELETE_SELF: Log.i("RecursiveFileObserver", "DELETE_SELF: " + path); break; case FileObserver.MODIFY: Log.i("RecursiveFileObserver", "MODIFY: " + path); break; case FileObserver.MOVE_SELF: Log.i("RecursiveFileObserver", "MOVE_SELF: " + path); break; case FileObserver.MOVED_FROM: Log.i("RecursiveFileObserver", "MOVED_FROM: " + path); break; case FileObserver.MOVED_TO: Log.i("RecursiveFileObserver", "MOVED_TO: " + path); break; case FileObserver.OPEN: Log.i("RecursiveFileObserver", "OPEN: " + path); break; default: Log.i("RecursiveFileObserver", "DEFAULT(" + event + " : " + path); break; } } /** * Monitor single directory and dispatch all events to its parent, with full * path. */ class SingleFileObserver extends FileObserver { String mPath; public SingleFileObserver(String path) { this(path, ALL_EVENTS); mPath = path; } public SingleFileObserver(String path, int mask) { super(path, mask); mPath = path; } @Override public void onEvent(int event, String path) { String newPath = mPath + "/" + path; MultiFileObserver.this.onEvent(event, newPath); } } }

优秀的个人博客,低调大师

android intent 最大传送内容 1m

如果Binder的参数或返回值太大,不适合的事务缓冲区,然后调用将失败,并将被抛出TransactionTooLargeException 参考:http://developer.android.com/reference/android/os/TransactionTooLargeException.html The Binder transaction failed because it was too large. During a remote procedure call, the arguments and the return value of the call are transferred asParcelobjects stored in the Binder transaction buffer. If the arguments or the return value are too large to fit in the transaction buffer, then the call will fail andTransactionTooLargeExceptionwill be thrown. The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size. There are two possible outcomes when a remote procedure call throwsTransactionTooLargeException. Either the client was unable to send its request to the service (most likely if the arguments were too large to fit in the transaction buffer), or the service was unable to send its response back to the client (most likely if the return value was too large to fit in the transaction buffer). It is not possible to tell which of these outcomes actually occurred. The client should assume that a partial failure occurred. The key to avoidingTransactionTooLargeExceptionis to keep all transactions relatively small. Try to minimize the amount of memory needed to create aParcelfor the arguments and the return value of the remote procedure call. Avoid transferring huge arrays of strings or large bitmaps. If possible, try to break up big requests into smaller pieces. If you are implementing a service, it may help to impose size or complexity contraints on the queries that clients can perform. For example, if the result set could become large, then don't allow the client to request more than a few records at a time. Alternately, instead of returning all of the available data all at once, return the essential information first and make the client ask for additional information later as needed 捐助开发者 在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。 谢谢您的赞助,我会做的更好!

优秀的个人博客,低调大师

「软件包管理 + 上架自检」双管齐下,提升上架审核通过率

还在为应用上架反复碰壁而头疼?别担心,HUAWEI AppGallery Connect 云测试推出的“软件包管理--上架自检”能力,通过前置合规校验、模拟真实环境测试并出具详细的测试报告。 平台配置多种型号鸿蒙真机,覆盖主流/热门机型,满足多样化测试场景需求。每天提供 300 分钟的免费使用时间,足够支撑新手尝鲜、轻量级项目测试或多次验证,0 成本起步测试,立省真机购买投入! 一、解锁新技能:这个 “应用管家” 不一般 全新上线的“软件包管理”能力,从应用提交伊始即提供全流程辅助,像一个贴心的私人“应用管家”。开发者上传软件包后,系统将自动进行基础合法性检测,校验其是否符合鸿蒙生态基本规范。支持开发者按需选择“仅测试”或“测试及正式上架”场景,满足不同阶段的验证需求。上传后即刻获取软件包合规状态(“已达标” “待优化”或“不通过”),清晰呈现应用与上架基础要求的差距。 更为核心的是“上架自检”功能:严格参照华为应用市场准入标准,在热门的移动终端设备上,对应用的兼容性、稳定性、性能、功耗及用户体验(UX) 进行全面自动化检测,让开发者在上架前及时发现并修复问题。每次检测均生成详尽报告,明确罗列问题点并提供优化建议,大幅减少反复修改的成本和时间。 二、操作指南:三步高效管理软件包 1.登录配置: 登录AppGallery Connect,点击“APP”,选择要发布的应用。 导航入口: 在左侧导航栏选择 “应用上架 > 软件包管理”。 2.上传与确认: 点击右上角“上传”,选择应用使用场景(“仅测试”或“测试和正式上架”),添加软件包文件。 3.结果解读:上传成功后,页面生成软件包记录。您可在“合法性”栏查看合法性检测结果。 ·已达标: 符合鸿蒙生态规范,可提交上架。点击“报告”可查看详细的检测报告。 ·待优化: 可尝试提交上架,但存在驳回风险。建议点击“报告”查看并修复问题。 ·不通过: 不满足上架基本要求,不允许上架,需根据报告建议修改后重新上传。 三、进阶技巧:上架自检,为应用质量加码 强烈建议开发者充分利用“上架自检”功能,为应用质量增加一层保障。 1.在软件包记录的“操作”列点击“启动自检”。 请注意: 每个应用同时仅允许一个自检任务运行;即使删除相关软件包,启动自检仍会计入服务使用配额。 2.启动后在“上架自检”栏可查看状态及结果: ·检测中:表示正在检测软件包,检测时长可能受终端设备数量和排队情况影响。 ·通过: 可安心提交上架申请。 待优化: 虽可提交,但驳回风险较高,强烈建议按报告指引进行优化。 HUAWEI AppGallery Connect 云测试提供海量鸿蒙真机在线免预约,可全面检测应用兼容性、性能、稳定性、功耗及 UX 等关键指标,首次试用赠 300 分钟!轻量测试 0 成本起步,极简操作,高效输出报告。成本低、易上手,点此立即试用 >> AppGallery Connect 致力于为应用的创意、开发、分发、运营、经营各环节提供一站式服务,构建全场景智慧化的应用生态体验。为给您带来更好服务,请扫描下方二维码或者点击此处免费咨询。 如您有任何疑问,请发送邮件至 agconnect@huawei.com 咨询,感谢您对 HUAWEI AppGallery Connect 的支持!

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册