标签:转换 file 通过 算法 ica long protect rgb size
其实我原本是只想说一个线程之间的通讯 handler,但是觉得内容有点少,就直接写了个demo。我之前是做过仿手机管家这种软件的(当然只是自己做着好玩的),所以 就提取了一点内容结合线程通讯写了个小的demo.
不说废话了,直接上代码:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ehsure.handlerdemo.MainActivity">
<TextView
android:id="@+id/main_change_tv"
android:gravity="center"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/main_scan_tv"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="16sp"
android:layout_height="wrap_content"
/>
</LinearLayout>
MianActivity.class
public class MainActivity extends Activity {
private List<String> appNameList = new ArrayList<String>();
public int backAppSize = 0;
TextView main_scan_tv;
TextView main_change_tv;
LinearLayout ll_main_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWidget();
new MyThread().start();
}
public void initWidget(){
main_change_tv = (TextView)findViewById(R.id.main_change_tv);
main_scan_tv = (TextView)findViewById(R.id.main_scan_tv);
ll_main_layout = (LinearLayout)findViewById(R.id.ll_main_layout);
}
/*
* 线程扫描所有后台软件
*/
class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
try {
PackageManager pm = MainActivity.this.getPackageManager();
ActivityManager activityManager = (ActivityManager) MainActivity.this
.getSystemService(MainActivity.this.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> runningApp = activityManager
.getRunningServices(100);//这个数字是设置的最大程序数
Iterator<ActivityManager.RunningServiceInfo> iterator = runningApp.iterator();
int allCount = 0;//这个是所有后台软件的总大小
while (iterator.hasNext()) {
ActivityManager.RunningServiceInfo backApp = iterator.next();
ComponentName name = backApp.service;
String pkgName = name.getPackageName();
ApplicationInfo apps = pm.getApplicationInfo(pkgName, 0);
String appName = (String) apps.loadLabel(pm);// 拿到名字
Drawable appIcon = apps.loadIcon(pm);// 拿到图标
String appPath = apps.publicSourceDir;// 拿到app的路径
int length = (int) new File(appPath).length();
allCount+=length;
SystemClock.sleep(100);
String out = appName+"~"+getFileSize(allCount);//这个方法是将字节转换成kb或mb,自己写的一个小算法
//这里之所以这么写 是因为一款软件可能会有好几个进程,如果不根据名字来判断,会有很多重复的。
boolean flag = true;
for (String ss : appNameList) {
if(ss.equals(appName)){
flag = false;
}
}
if(flag==true){
appNameList.add(appName);
}
handler.obtainMessage(0x001, out).sendToTarget();
}
backAppSize = allCount;
SystemClock.sleep(2000);
handler.sendEmptyMessage(0x002);
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 将字节转换成kb或mb
*/
public String getFileSize(long size){
String mySize = "";
if(size>(1024*1024)){
BigDecimal bd = new BigDecimal((double)size/(1024*1024));
BigDecimal bd2 =bd.setScale(2, BigDecimal.ROUND_DOWN);
mySize = (Double.parseDouble(bd2+""))+"M";
}else{
mySize = (size/1024)+"k";
}
return mySize;
}
Handler handler = new Handler(){
public void handleMessage(Message msg) {
int code = msg.what;
switch (code) {
case 0x001:
String str = (String) msg.obj;
String [] ss = str.split("~");
main_scan_tv.setText(ss[0]);
main_change_tv.setText(ss[1]);
break;
case 0x002:
main_scan_tv.setText("一共发现" + appNameList.size() + "款后台软件");
ll_main_layout.setBackgroundColor(Color.rgb(10, 163, 245));
break;
}
}
};
}
我这里用到的handler发送消息用了两种方式,当然也是推荐的
1.
handler.sendEmptyMessage(0x002);这个就是不传递消息或者数据的
2.
handler.obtainMessage(0x001, out).sendToTarget();这个也就是最常用的一种,是传递Object对象过去
其实还有一种:
Message msg = new Message();
msg.what =0x001;
msg.obj = out;
handler.sendMessage(msg);
但是这种方式 handler传递多少次消息 Message就会实例化多少个,所以不太推荐,因为会造成内存损耗。
通过线程通讯获取手机正在运行中的软件及大小(仿手机管家 扫描软件)
标签:转换 file 通过 算法 ica long protect rgb size
原文地址:http://www.cnblogs.com/carrie124/p/carrie_handler.html