我正在研究家长控制/成人内容过滤应用程序.这个应用程序持续监视孩子的手机上的电话和微笑,并将所有活动记录到服务器上.为此,我在BOOT_COMPLETED上启动服务(MyService.java),在服务的onCreate方法中,我为callLog和sms uri注册了一个contentobserver(请参阅下面的代码片段).
现在的问题是,因为我想监视每个传出,传入呼叫和短信我希望服务连续运行(不被停止/杀死).此外,此服务仅用于注册内容观察者而不进行任何其他处理(其OnstartCommand方法是虚拟的),因此android OS会在一段时间后终止服务.如何确保服务连续运行并使contentobserver对象保持活动状态?
public class MyService extends Service {
private CallLogObserver clLogObs = null;
public void onCreate() {
super.onCreate();
try{
clLogObs = new CallLogObserver(this);
this.getContentResolver().registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI, true, clLogObs);
}catch(Exception ex)
{
Log.e("CallLogData", ex.toString());
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
if( clLogObs !=null )
{
this.getContentResolver().unregisterContentObserver(clLogObs);
}
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
} … 我正在编写一个应用程序,可以编程方式清除设备上安装的所有第三方应用程序的应用程序缓存.以下是Android 2.2的代码段
public static void trimCache(Context myAppctx) {
Context context = myAppctx.createPackageContext("com.thirdparty.game",
Context.CONTEXT_INCLUDE_CO|Context.CONTEXT_IGNORE_SECURITY);
File cachDir = context.getCacheDir();
Log.v("Trim", "dir " + cachDir.getPath());
if (cachDir!= null && cachDir.isDirectory()) {
Log.v("Trim", "can read " + cachDir.canRead());
String[] fileNames = cachDir.list();
//Iterate for the fileName and delete
}
}
Run Code Online (Sandbox Code Playgroud)
我的清单有以下权限:
android.permission.CLEAR_APP_CACHE
android.permission.DELETE_CACHE_FILES
现在的问题是打印了缓存目录的名称,但文件列表cachDir.list()始终返回null.我无法删除缓存目录,因为文件列表始终为null.
还有其他方法可以清除应用程序缓存吗?
android ×2