您的位置:首页 > 编程语言

获取手机所有apk,部分代码片段

2015-05-11 14:56 211 查看
private void loadAllAppsByBatch() {

final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;

// Don't use these two variables in any of the callback runnables.

// Otherwise we hold a reference to them.

final Callbacks oldCallbacks = mCallbacks.get();

if (oldCallbacks == null) {

// This launcher has exited and nobody bothered to tell us. Just bail.

Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");

return;

}

final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);

mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

final PackageManager packageManager = mContext.getPackageManager();

List<ResolveInfo> apps = null;

int N = Integer.MAX_VALUE;

int startIndex;

int i=0;

int batchSize = -1;

while (i < N && !mStopped) {

if (i == 0) {

mBgAllAppsList.clear();

final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;

apps = packageManager.queryIntentActivities(mainIntent, 0);

if (DEBUG_LOADERS) {

Log.d(TAG, "queryIntentActivities took "

+ (SystemClock.uptimeMillis()-qiaTime) + "ms");

}

if (apps == null) {

return;

}

N = apps.size();

if (DEBUG_LOADERS) {

Log.d(TAG, "queryIntentActivities got " + N + " apps");

}

if (N == 0) {

// There are no apps?!?

return;

}

if (mBatchSize == 0) {

batchSize = N;

} else {

batchSize = mBatchSize;

}

final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;

Collections.sort(apps,

new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));

if (DEBUG_LOADERS) {

Log.d(TAG, "sort took "

+ (SystemClock.uptimeMillis()-sortTime) + "ms");

}

}

final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;

startIndex = i;

for (int j=0; i<N && j<batchSize; j++) {

// This builds the icon bitmaps.

mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),

mIconCache, mLabelCache));

i++;

}

final boolean first = i <= batchSize;

final Callbacks callbacks = tryGetCallbacks(oldCallbacks);

final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;

mBgAllAppsList.added = new ArrayList<ApplicationInfo>();

mHandler.post(new Runnable() {

public void run() {

final long t = SystemClock.uptimeMillis();

if (callbacks != null) {

if (first) {

callbacks.bindAllApplications(added);

} else {

callbacks.bindAppsAdded(added);

}

if (DEBUG_LOADERS) {

Log.d(TAG, "bound " + added.size() + " apps in "

+ (SystemClock.uptimeMillis() - t) + "ms");

}

} else {

Log.i(TAG, "not binding apps: no Launcher activity");

}

}

});

if (DEBUG_LOADERS) {

Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "

+ (SystemClock.uptimeMillis()-t2) + "ms");

}

if (mAllAppsLoadDelay > 0 && i < N) {

try {

if (DEBUG_LOADERS) {

Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");

}

Thread.sleep(mAllAppsLoadDelay);

} catch (InterruptedException exc) { }

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: