public static void testProvide() { IPackageDataObserver packageDataObserver = new IPackageDataObserver() { @Override public void onRemoveCompleted(String s, boolean b) throws RemoteException { doHeavyWork(); doHeavyWork(); } @Override public IBinder asBinder() { return null; } }; try { packageDataObserver.onRemoveCompleted("haha", false); } catch (RemoteException e) { e.printStackTrace(); } doHeavyWork(); }
@Override protected Long doInBackground(Void... params) { final CountDownLatch countDownLatch = new CountDownLatch(1); StatFs stat = new StatFs( Environment.getDataDirectory().getAbsolutePath()); try { mFreeStorageAndNotifyMethod.invoke(getPackageManager(), (long) stat.getBlockCount() * (long) stat.getBlockSize(), new IPackageDataObserver.Stub() { @Override public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException { countDownLatch.countDown(); } }); countDownLatch.await(); } catch (InvocationTargetException | InterruptedException | IllegalAccessException e) { e.printStackTrace(); } return mCacheSize; }
@Override public void onCreate() { try { mGetPackageSizeInfoMethod = getPackageManager().getClass() .getMethod( "getPackageSizeInfo", String.class, IPackageStatsObserver.class); mFreeStorageAndNotifyMethod = getPackageManager().getClass() .getMethod( "freeStorageAndNotify", long.class, IPackageDataObserver.class); mDeleteApplicationCacheFiles = getPackageManager().getClass() .getMethod( "deleteApplicationCacheFiles", String.class, IPackageDataObserver.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } }
/** * clear all app cache * @param context * @param listener the completion listener, it can be null if you don't care the result. * onClearCompleted run on child thread. * onClearFailed run on UI thread. */ public static void clearAllCache(Context context, final ClearCacheListener listener) { /* public void freeStorageAndNotify(long freeStorageSize, IPackageDataObserver observer) { freeStorageAndNotify(null, freeStorageSize, observer); } */ PackageManager pm = context.getPackageManager(); try { Method freeStorageAndNotify = pm.getClass().getDeclaredMethod("freeStorageAndNotify", long.class, IPackageDataObserver.class); freeStorageAndNotify.invoke(pm, Long.MAX_VALUE, new IPackageDataObserver.Stub(){ @Override public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException { if(null != listener) listener.onClearCompleted(); } }); } catch (Exception e) { e.printStackTrace(); if(null != listener) listener.onClearFailed(); } }
@Override protected Long doInBackground(Void... params) { final CountDownLatch countDownLatch = new CountDownLatch(1); StatFs stat = new StatFs(Environment.getDataDirectory().getAbsolutePath()); try { mFreeStorageAndNotifyMethod.invoke(getPackageManager(), (long) stat.getBlockCount() * (long) stat.getBlockSize(), new IPackageDataObserver.Stub() { @Override public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException { countDownLatch.countDown(); } } ); countDownLatch.await(); } catch (InvocationTargetException | InterruptedException | IllegalAccessException e) { e.printStackTrace(); } return mCacheSize; }
/** * ����¼���һ�������� */ public void cleanAll(View view) { try { Method method = PackageManager.class.getDeclaredMethod( "freeStorageAndNotify", long.class, IPackageDataObserver.class); // ����Ҫ�ͷŵĿռ��С long localLong = Long.valueOf(getEnviromenSize() - 1L); // method.invoke(pm, localLong, new MyIPackageDataObserver()); // ֱ�Ӹ�һ��Long.MAX_VALUEҲ�� method.invoke(pm, Long.MAX_VALUE, new MyIPackageDataObserver()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(context, "ȫ��������", Toast.LENGTH_LONG).show(); fillData(); }
@Override public Object call(Object who, Method method, Object... args) throws Throwable { if (args[args.length - 1] instanceof IPackageDataObserver) { IPackageDataObserver observer = (IPackageDataObserver) args[args.length - 1]; observer.onRemoveCompleted(null, true); } return 0; }
private void initUI() { bt_clear = (Button) findViewById(R.id.bt_clear); pb_bar = (ProgressBar) findViewById(R.id.pb_bar); tv_name = (TextView) findViewById(R.id.tv_name); ll_add_text = (LinearLayout) findViewById(R.id.ll_add_text); bt_clear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //1.��ȡָ������ֽ����ļ� try { Class<?> clazz = Class.forName("android.content.pm.PackageManager"); //2.��ȡ���÷������� Method method = clazz.getMethod("freeStorageAndNotify", long.class,IPackageDataObserver.class); //3.��ȡ������÷��� method.invoke(mPm, Long.MAX_VALUE,new IPackageDataObserver.Stub() { @Override public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException { //���������ɺ���õķ���(����Ȩ��) Message msg = Message.obtain(); msg.what = CLEAR_CACHE; mHandler.sendMessage(msg); } }); } catch (Exception e) { e.printStackTrace(); } } }); }
@Override public void onCreate() { try { mGetPackageSizeInfoMethod = getPackageManager().getClass() .getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); mFreeStorageAndNotifyMethod = getPackageManager().getClass() .getMethod("freeStorageAndNotify", long.class, IPackageDataObserver.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } }
@Override public void onCreate() { try { mGetPackageSizeInfoMethod = getPackageManager().getClass().getMethod( "getPackageSizeInfo", String.class, IPackageStatsObserver.class); mFreeStorageAndNotifyMethod = getPackageManager().getClass().getMethod( "freeStorageAndNotify", long.class, IPackageDataObserver.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } }
/** @hide */ @Override public void clearApplicationUserData(String packageName, IPackageDataObserver observer) { mBase.clearApplicationUserData(packageName, observer); }
/** @hide */ @Override public void deleteApplicationCacheFiles(String packageName, IPackageDataObserver observer) { mBase.deleteApplicationCacheFiles(packageName, observer); }
/** @hide */ @Override public void deleteApplicationCacheFilesAsUser(String packageName, int userId, IPackageDataObserver observer) { mBase.deleteApplicationCacheFilesAsUser(packageName, userId, observer); }
/** @hide */ @Override public void freeStorageAndNotify(long freeStorageSize, IPackageDataObserver observer) { mBase.freeStorageAndNotify(freeStorageSize, observer); }
/** @hide */ @Override public void freeStorageAndNotify(String volumeUuid, long freeStorageSize, IPackageDataObserver observer) { mBase.freeStorageAndNotify(volumeUuid, freeStorageSize, observer); }
@Override public void clearApplicationUserData(String packageName, IPackageDataObserver observer) { }
@Override public void deleteApplicationCacheFiles(String packageName, IPackageDataObserver observer) { }
@Override public void freeStorageAndNotify(long freeStorageSize, IPackageDataObserver observer) { }