android.app.ActivityManager#getMemoryClass ( )源码实例Demo

下面列出了android.app.ActivityManager#getMemoryClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: arcusandroid   文件: ImageManager.java
/**
 * Enables or disables the disk cache for the life of the application. This method cannot be
 * called more than once per lifetime of the app and should therefore be called only during
 * application startup; subsequent calls have no effect on the disk cache state.
 *
 * Note that when enabling Piccaso cache indicators, you may still find that some images appear
 * as though they've been loaded from disk. This will be true for any app-packaged drawable or
 * bitmap resource that's placed by Picasso. (These are always "from disk" with or without the
 * presence of a disk cache.) Similarly, it's possible to get green-tagged images when using
 * Picasso to "load" manually pre-loaded BitmapDrawables. 
 *
 * @param diskCacheEnabled
 */
public static void setConfiguration(Context context, boolean diskCacheEnabled, Integer cacheHeapPercent) {
    try {

        Picasso.Builder builder = new Picasso.Builder((ArcusApplication.getContext()));

        if (diskCacheEnabled) {
            builder.downloader(new OkHttp3Downloader(new OkHttpClient()));
        }

        if (cacheHeapPercent != null) {
            ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
            int memoryClass = am.getMemoryClass();

            int heapSize = (int) ((float)(1024 * 1024 * memoryClass) * ((float) cacheHeapPercent / 100.0));
            builder.memoryCache(new LruCache(heapSize));
            logger.debug("Setting Picasso in-memory LRU cache max size to {} bytes; {}% of heap sized {}", heapSize, cacheHeapPercent, 1024 * 1024 * memoryClass);
        }

        Picasso.setSingletonInstance(builder.build());

    } catch (IllegalStateException e) {
        logger.warn("Picasso setConfiguration() has already been called; ignoring request.");
    }
}
 
public CachingAsyncImageLoader(Context context) {
    ActivityManager am = (ActivityManager)context.getSystemService(
            Context.ACTIVITY_SERVICE);
    int memoryClass = (am.getMemoryClass() * 1024 * 1024) / CACHE_DIVISOR;        //basically, set the heap to be everything we can get
    this.context = context;
    this.cache = new LruCache<>(memoryClass);
}
 
源代码3 项目: EhViewer   文件: BitmapUtils.java
public static long availableMemory() {
    final Runtime runtime = Runtime.getRuntime();
    final long used = runtime.totalMemory() - runtime.freeMemory();

    final ActivityManager activityManager = (ActivityManager) sContext.
            getSystemService(Context.ACTIVITY_SERVICE);
    final long total = activityManager.getMemoryClass() * 1024 * 1024;

    return total - used;
}
 
源代码4 项目: WliveTV   文件: DefaultConfigurationFactory.java
/**
 * Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
 * Default cache size = 1/8 of available app memory.
 */
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
	if (memoryCacheSize == 0) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		int memoryClass = am.getMemoryClass();
		if (hasHoneycomb() && isLargeHeap(context)) {
			memoryClass = getLargeMemoryClass(am);
		}
		memoryCacheSize = 1024 * 1024 * memoryClass / 8;
	}
	return new LruMemoryCache(memoryCacheSize);
}
 
源代码5 项目: FairEmail   文件: FragmentOptionsMisc.java
private void setOptions() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());

    PackageManager pm = getContext().getPackageManager();
    int state = pm.getComponentEnabledSetting(new ComponentName(getContext(), ActivitySearch.class));

    swExternalSearch.setChecked(state != PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
    swShortcuts.setChecked(prefs.getBoolean("shortcuts", true));
    swFts.setChecked(prefs.getBoolean("fts", false));
    swEnglish.setChecked(prefs.getBoolean("english", false));
    swWatchdog.setChecked(prefs.getBoolean("watchdog", true));
    swUpdates.setChecked(prefs.getBoolean("updates", true));
    swUpdates.setVisibility(
            Helper.isPlayStoreInstall() || !Helper.hasValidFingerprint(getContext())
                    ? View.GONE : View.VISIBLE);
    swExperiments.setChecked(prefs.getBoolean("experiments", false));
    swQueries.setChecked(prefs.getInt("query_threads", 4) < 4);
    swCrashReports.setChecked(prefs.getBoolean("crash_reports", false));
    tvUuid.setText(prefs.getString("uuid", null));
    swDebug.setChecked(prefs.getBoolean("debug", false));
    swAuthSasl.setChecked(prefs.getBoolean("auth_sasl", true));
    swCleanupAttachments.setChecked(prefs.getBoolean("cleanup_attachments", false));

    tvProcessors.setText(getString(R.string.title_advanced_processors, Runtime.getRuntime().availableProcessors()));

    ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
    int class_mb = am.getMemoryClass();
    tvMemoryClass.setText(getString(R.string.title_advanced_memory_class, class_mb + " MB"));

    tvStorageSpace.setText(getString(R.string.title_advanced_storage_space,
            Helper.humanReadableByteCount(Helper.getAvailableStorageSpace(), true),
            Helper.humanReadableByteCount(Helper.getTotalStorageSpace(), true)));
    tvFingerprint.setText(Helper.getFingerprint(getContext()));

    grpDebug.setVisibility(swDebug.isChecked() || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
}
 
源代码6 项目: picasso   文件: Utils.java
static int calculateMemoryCacheSize(Context context) {
  ActivityManager am = ContextCompat.getSystemService(context, ActivityManager.class);
  boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
  int memoryClass = largeHeap ? am.getLargeMemoryClass() : am.getMemoryClass();
  // Target ~15% of the available heap.
  return (int) (1024L * 1024L * memoryClass / 7);
}
 
源代码7 项目: DoraemonKit   文件: Utils.java
static int calculateMemoryCacheSize(Context context) {
  ActivityManager am = getService(context, ACTIVITY_SERVICE);
  boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
  int memoryClass = am.getMemoryClass();
  if (largeHeap && SDK_INT >= HONEYCOMB) {
    memoryClass = ActivityManagerHoneycomb.getLargeMemoryClass(am);
  }
  // Target ~15% of the available heap.
  return 1024 * 1024 * memoryClass / 7;
}
 
/**
 * Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
 * Default cache size = 1/8 of available app memory.
 */
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
	if (memoryCacheSize == 0) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		int memoryClass = am.getMemoryClass();
		if (hasHoneycomb() && isLargeHeap(context)) {
			memoryClass = getLargeMemoryClass(am);
		}
		memoryCacheSize = 1024 * 1024 * memoryClass / 8;
	}
	return new LruMemoryCache(memoryCacheSize);
}
 
源代码9 项目: bubble   文件: Utils.java
public static int getHeapSize(Context context) {
    ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    boolean isLargeHeap = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
    int memoryClass = am.getMemoryClass();
    if (isLargeHeap && Utils.isHoneycombOrLater()) {
        memoryClass = am.getLargeMemoryClass();
    }
    return 1024 * memoryClass;
}
 
源代码10 项目: Aurora   文件: CacheType.java
@Override
public int calculateCacheSize(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
    if (targetMemoryCacheSize >= MAX_SIZE) {
        return MAX_SIZE;
    }
    return targetMemoryCacheSize;
}
 
源代码11 项目: MVPArms   文件: CacheType.java
@Override
public int calculateCacheSize(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
    if (targetMemoryCacheSize >= MAX_SIZE) {
        return MAX_SIZE;
    }
    return targetMemoryCacheSize;
}
 
源代码12 项目: deltachat-android   文件: LogViewFragment.java
@TargetApi(VERSION_CODES.KITKAT)
public static String getMemoryClass(Context context) {
  ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  String          lowMem          = "";

  if (VERSION.SDK_INT >= VERSION_CODES.KITKAT && activityManager.isLowRamDevice()) {
    lowMem = ", low-mem device";
  }
  return activityManager.getMemoryClass() + lowMem;
}
 
源代码13 项目: SoBitmap   文件: Util.java
static int getAvailableMemorySize(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int ret = -1;
    boolean largeHeap = false;
    if (Build.VERSION.SDK_INT >= 11) {
        largeHeap = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
        if (largeHeap)
            ret = am.getLargeMemoryClass() * 1024;
    }
    if (!largeHeap || Build.VERSION.SDK_INT < 11) {
        ret = am.getMemoryClass() * 1024;
    }
    return ret / 5;
}
 
源代码14 项目: FireFiles   文件: DocumentsApplication.java
@Override
public void onCreate() {
    super.onCreate();
    if(!BuildConfig.DEBUG) {
        AnalyticsManager.intialize(getApplicationContext());
    }
    sInstance = this;
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;

    mRoots = new RootsCache(this);
    mRoots.updateAsync();

    mSAFManager = new SAFManager(this);

    mThumbnails = new ThumbnailCache(memoryClassBytes / 4);

    final IntentFilter packageFilter = new IntentFilter();
    packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
    packageFilter.addDataScheme("package");
    registerReceiver(mCacheReceiver, packageFilter);

    final IntentFilter localeFilter = new IntentFilter();
    localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
    registerReceiver(mCacheReceiver, localeFilter);

    isTelevision = Utils.isTelevision(this);
}
 
源代码15 项目: MVPArms   文件: CacheType.java
@Override
public int calculateCacheSize(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
    if (targetMemoryCacheSize >= MAX_SIZE) {
        return MAX_SIZE;
    }
    return targetMemoryCacheSize;
}
 
源代码16 项目: MVPArms   文件: CacheType.java
@Override
public int calculateCacheSize(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
    if (targetMemoryCacheSize >= MAX_SIZE) {
        return MAX_SIZE;
    }
    return targetMemoryCacheSize;
}
 
/**
 * Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
 * Default cache size = 1/8 of available app memory.
 */
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
    if (memoryCacheSize == 0) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        int memoryClass = am.getMemoryClass();
        if (hasHoneycomb() && isLargeHeap(context)) {
            memoryClass = getLargeMemoryClass(am);
        }
        memoryCacheSize = 1024 * 1024 * memoryClass / 8;
    }
    return new LruMemoryCache(memoryCacheSize);
}
 
源代码18 项目: CrossBow   文件: DefaultCrossbowComponents.java
public CrossbowImageCache onCreateImageCache() {
    ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
    int memCacheSize = ((am.getMemoryClass() * 1024 * 1024) / 10);
    return new DefaultImageCache(memCacheSize);
}
 
源代码19 项目: sketch   文件: MemorySizeCalculator.java
private static int getMaxSize(@Nullable ActivityManager activityManager) {
    final int memoryClassBytes = activityManager != null ? activityManager.getMemoryClass() * 1024 * 1024 : 100;
    final boolean isLowMemoryDevice = isLowMemoryDevice(activityManager);
    return Math.round(memoryClassBytes
            * (isLowMemoryDevice ? LOW_MEMORY_MAX_SIZE_MULTIPLIER : MAX_SIZE_MULTIPLIER));
}
 
源代码20 项目: MemoryMonitor   文件: MemoryUtil.java
/**
 * 获取应用能够获取的max dalvik堆内存大小
 * 和Runtime.getRuntime().maxMemory()一样
 *
 * @param context
 * @return 单位M
 */
public static long getAppTotalDalvikHeapSize(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    return manager.getMemoryClass();
}