標籤:android aidl
1、把下面兩個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;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);}
/* //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;
2、在activity介面利用Java反射實現
package com.example.yqqmobilesafe.cleanache;import java.lang.reflect.Method;import java.util.List;import android.app.Activity;import android.content.Intent;import android.content.pm.IPackageStatsObserver;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.content.pm.PackageStats;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.RemoteException;import android.text.format.Formatter;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ProgressBar;import android.widget.TextView;import com.example.yqqmobilesafe.R;/** * 清理緩衝 * @author yqq * */public class CleanCacheActivity extends Activity {private PackageManager pm;private LinearLayout mContainer;//用來存放待清理垃圾的應用private ProgressBar mProgressBar;//顯示掃描的進度private TextView tvScanAppName;//應用程式名稱private final int SCANING_FINISH=1;//掃描應用結束private final int SCANNING=2;//掃描中//private MyPackageInfo info;private Handler mHandler=new Handler(){@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case SCANING_FINISH:tvScanAppName.setText("掃描完成");break;case SCANNING://返回應用的資訊final MyPackageInfo info=(MyPackageInfo) msg.obj;//獲得應用程式名稱//String appName=info.packName;tvScanAppName.setText("正在掃描:"+info.packName);if(info.cacheSize>0){//將應用展示在介面容器上View view=View.inflate(CleanCacheActivity.this,R.layout.app_cache_info_item,null);//設定事件監聽view.setClickable(true);view.setBackgroundResource(R.drawable.ache_clear_item_bg_selector);//清空快取資料view.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 開啟設定介面Intent intent = new Intent();intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");intent.setData(Uri.parse("package:"+info.packName ));startActivity(intent);}});ImageView appIconIV=(ImageView) view.findViewById(R.id.iv_app_icon);TextView appNameTV=(TextView) view.findViewById(R.id.tv_app_name);TextView appCacheSizeTV=(TextView) view.findViewById(R.id.tv_app_cache_size);TextView appDataSizeTV=(TextView) view.findViewById(R.id.tv_app_code_size);//設定顯示資料try {appIconIV.setImageDrawable(pm.getApplicationInfo(info.packName, 0).loadIcon(pm));} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}appNameTV.setText(info.packName);appCacheSizeTV.setText("緩衝大小:"+Formatter.formatFileSize(getApplicationContext(), info.cacheSize));appDataSizeTV.setText("資料大小"+Formatter.formatFileSize(getApplicationContext(), info.dataSize));mContainer.addView(view,0);}break;}super.handleMessage(msg);}};public CleanCacheActivity() {}@Overrideprotected void onCreate(Bundle savedInstanceState) {setContentView(R.layout.activity_cache_clear);init();super.onCreate(savedInstanceState);}/** * 初始化資訊 */private void init() {mContainer=(LinearLayout) this.findViewById(R.id.ll_container);tvScanAppName=(TextView) this.findViewById(R.id.tv_scaning_app_name);mProgressBar=(ProgressBar) this.findViewById(R.id.pb);pm=getPackageManager();scanAppCache();}/** * 掃描應用獲得待清理的應用 */private void scanAppCache(){new Thread(new Runnable() {@Overridepublic void run() {List<PackageInfo> infos=pm.getInstalledPackages(0);//獲得已經安裝應用的資訊mProgressBar.setMax(infos.size());//設定進度條的最大數目int total=0;for(PackageInfo info:infos){getPackageSize(info.packageName);try {Thread.sleep(300);total++;mProgressBar.setProgress(total);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}Message message=Message.obtain();message.what=SCANING_FINISH;//掃描完成mHandler.sendMessage(message);}}).start();}/** * 通過反射獲得應用的大小 * @param packName應用的包名 */private void getPackageSize(String packName){try {Method method=PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class,IPackageStatsObserver.class});method.invoke(pm,new Object[]{packName,new MyObserver(packName)}); } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 定義一個包狀態觀察者獲得資料緩衝,代碼大小,資料大小 * @author yqq * */private class MyObserver extends IPackageStatsObserver.Stub{private String packName;//應用的包名public MyObserver(String packName) {super();this.packName = packName;}@Overridepublic void onGetStatsCompleted(PackageStats pStats, boolean succeeded)throws RemoteException {//獲得每個應用的大小MyPackageInfo myPackageInfo=new MyPackageInfo();myPackageInfo.dataSize=pStats.dataSize;myPackageInfo.cacheSize=pStats.cacheSize;myPackageInfo.packName=packName;Message message=Message.obtain();message.obj=myPackageInfo;message.what=SCANNING;mHandler.sendMessage(message);myPackageInfo=null;}}private class MyPackageInfo{String packName;//應用的包名long dataSize;//資料大小long cacheSize;//緩衝大小}}
android AIDL實踐之清理應用緩衝