Common tool classes, file and memory size acquisition, shell script execution

Source: Internet
Author: User
Tags stringbuffer

/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */package Android.cts.util;import Android.app.activitymanager;import Android.app.ActivityManager.MemoryInfo; Import Android.app.instrumentation;import Android.content.context;import Android.os.parcelfiledescriptor;import Android.os.statfs;import Java.io.fileinputstream;import Java.io.ioexception;public class SystemUtil {public static lo Ng Getfreedisksize (Context context) {StatFs statFs = new StatFs (Context.getfilesdir (). GetAbsolutePath ());    Return (long) statfs.getavailableblocks () * statfs.getblocksize ();        } public static long Getfreememory (context context) {Memoryinfo info = new Memoryinfo ();        Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service);        Activitymanager.getmemoryinfo (info);    return info.availmem;        } public static long Gettotalmemory (context context) {Memoryinfo info = new Memoryinfo ();        Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service);        Activitymanager.getmemoryinfo (info); return info.totalmem;    TODO totalmem N/A in ICS. }/** * Executes a shell command using shell user identity, and return the standard output in String * <p&gt ; Note:calling This function requires API level or above * @param instrumentation {@link instrumentation} instance, Obtained from a TEST running in * instrumentation framework * @param cmd the command to run * @return the standard output of th e command * @throws Exception */public static string Runshellcommand (instrumentation instrumentation, string C MD) throws IOException {Parcelfiledescriptor PFD = Instrumentation.getuiautomation (). Executeshellcomman        D (CMD);        byte[] buf = new byte[512];        int bytesread;        FileInputStream fis = new Parcelfiledescriptor.autocloseinputstream (PFD);        StringBuffer stdout = new StringBuffer ();        while ((Bytesread = Fis.read (BUF))! =-1) {Stdout.append (new String (buf, 0, bytesread));        } fis.close ();    return stdout.tostring (); }}

Common tool classes, file and memory size acquisition, shell script execution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.