編輯:關於Android編程
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(){
@Override
public 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() {
@Override
public 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 block
e.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() {
}
@Override
protected 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() {
@Override
public void run() {
List 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 block
e.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 block
e.printStackTrace();
}
}
/**
* 定義一個包狀態觀察者獲得數據緩存,代碼大小,數據大小
* @author yqq
*
*/
private class MyObserver extends IPackageStatsObserver.Stub{
private String packName;//應用的包名
public MyObserver(String packName) {
super();
this.packName = packName;
}
@Override
public 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實現帶磁性的懸浮窗體效果
本文實例講述了Android實現帶磁性的懸浮窗體效果。分享給大家供大家參考,具體如下:帶磁性的懸浮窗體,類似於360綠色小人主要實現的是:1.懸浮所有窗體之上2.有吸引力
Android實用工具類-GrallyAndPhotoUtils圖片處理工具
概述此類是用於簡便調用系統拍照及打開相冊選擇圖片.通用於多種機型.(親測魅族MX4,三星note 2,三星note 3)前言在執行拍照和打開相冊之前,我們需要注意一下.由
Android仿微信微博多圖展示效果
1.簡介這是一個用於實現像微信朋友圈和微博的類似的九宮格圖片展示控件,通過自定義viewgroup實現,使用方便。 多圖根據屏幕適配,單張圖片時需要自己指定圖片的寬高;2
cocos - js (v3.12) 搭建技術文章
下載必備的軟件包下載並安裝WebStorm7。WebStorm7目前的穩定版本是7.0.3。為什麼我們選擇WebStorm?因為它提供了許多功能,如JavaScript代