示例:
new Thread(){
private Message message;
public void run() {
PackageManager packageManager = getPackageManager();
List<PackageInfo> packages = packageManager.getInstalledPackages(0);
progressBar1.setMax(packages.size());
int progress = 0;
for (PackageInfo packageInfo : packages) {
ScanInfo scanInfo = new ScanInfo();
String appName = packageInfo.applicationInfo.loadLabel(packageManager).toString();
String packageName = packageInfo.packageName;
scanInfo.appName = appName;
scanInfo.packageName = packageName;
String sourceDir = packageInfo.applicationInfo.sourceDir;
String md5 = MD5Utils.getFileMd5(sourceDir);
String desc = AntivirusDao.checkFileVirus(md5);
System.out.println("-------------------------");
System.out.println(appName);
System.out.println(md5);
if(desc == null){
scanInfo.desc = false;
}else{
scanInfo.desc = true;
}
progress++;
progressBar1.setProgress(progress);
message = Message.obtain();
message.what = SCANNING;
message.obj = scanInfo;
handler.sendMessage(message);
}
message = Message.obtain();
message.what = FINISHED;
handler.sendMessage(message);
};
}.start();
线程?
答案 0 :(得分:0)
之前我确实喜欢这样:
HashSet
HashSet<MyThread> myThreads = new HashSet<>();
@Override
public void onDestroy() {
super.onDestroy();
if (myThreads.iterator().hasNext())
myThreads.iterator().next().interrupt();
}
private void doSomething() {
Runnable runnable = new Runnable() {
@Override
public void run() {
//doing some work
}
};
MyThread thread = new MyThread(runnable);
thread.run();
myThreads.add(thread);
}
class MyThread extends Thread {
public MyThread(Runnable runnable) {
super(runnable);
}
}
或其他内容来保存每个帖子
代码如下所示,希望对您有所帮助
{{1}}