編輯:關於Android編程
我們都知道在Android的設置->應用程序中可以查看應用程序的相關信息,其中有一個功能是清除緩存。如圖:
怎麼實現這些功能呢,從Android的setting源碼中可以得到相關信息。
實現如下:
Java代碼:
package com.wang.clearcache; import java.lang.reflect.Method; import android.os.Bundle; import android.os.RemoteException; import android.app.Activity; import android.content.pm.IPackageStatsObserver; import android.content.pm.PackageManager; import android.content.pm.PackageStats; public class MainActivity extends Activity { private PackageManager pm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pm = getPackageManager(); //反射 try { Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class,IPackageStatsObserver.class}); method.invoke(pm, new Object[]{"com.wang.clearcache",new IPackageStatsObserver.Stub() { @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { long cachesize = pStats.cacheSize; long codesize = pStats.codeSize; long datasize = pStats.dataSize; System.out.println("cachesize:"+ cachesize); System.out.println("codesize:"+ codesize); System.out.println("datasize"+ datasize); } }}); } catch (Exception e) { e.printStackTrace(); } } }
因為得到緩存信息需要加入android.permission.GET_PACKAGE_SIZE的權限
Androidmainifest.xml因為使用在代碼中使用了PackageManager的getPackageSizeInfo這個函數,但是這個方法是不對外公開的函數,所有我們需要使用發射來調用這個函數,在該方法的內部回調了onGetStatsCompleted(PackageStats pStats, boolean succeeded)這個方法,通過該方法的pStats參數可以得到應用的緩存,數據緩存,代碼容量緩存,在使用的過程中需要用到系統的aidl文件
IPackageStatsObserver:
/* ** ** Copyright 2007, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ package android.content.pm; import android.content.pm.PackageStats; /** * API for package data change related callbacks from the Package Manager. * Some usage scenarios include deletion of cache directory, generate * statistics related to code, data, cache usage(TODO) * {@hide} */ oneway interface IPackageStatsObserver { void onGetStatsCompleted(in PackageStats pStats, boolean succeeded); }
PackageStats:
/* //device/java/android/android/view/WindowManager.aidl ** ** Copyright 2007, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ package android.content.pm; parcelable PackageStats;
源碼地址下載:
http://download.csdn.net/detail/wangbiaohome/8026535
效果:點擊字體,字體變大 主要利用的getView()方法和setOnItemClickListener()方法 ListText.java 復制代碼 代碼如下: pac
實現如下邊框效果:虛線畫效果,可以使用Android中的xml來做。下面話不多說,直接上代碼:<RelativeLayout android:id=@+i
本篇博客主要給大家演示如何一步一步地創建一個類似於下圖展示的這麼一個UI界面: 一、准備圖片資源 第二步:ImageButton設置 源
前提條件:已經安裝了JDK 6.0、android SDK、NDK r9和eclipsele4.2開發環境.推薦下載Android開發的綜合套件adt-bundle-wi