標籤:轉換 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就會執行個體化多少個,所以不太推薦,因為會造成記憶體損耗。
通過線程通訊擷取手機正在運行中的軟體及大小(仿手機管家 掃描軟體)