Android programming SDcard related code collection _android

Source: Internet
Author: User

This example describes the SDcard code for Android programming. Share to everyone for your reference, specific as follows:

1. Detect SDcard is available:

public static Boolean sdcardisavailable () { 
  String status = Environment.getexternalstoragestate (); 
  if (!status.equals (environment.media_mounted)) {return 
   false; 
  } 
  return true; 
} 

2. Obtain the program on the SD card CAHCE directory:

private static Boolean Hasexternalcachedir () {return 
  Build.VERSION.SDK_INT >= build.version_codes. FROYO; 
} 
/** 
* @param context 
* @return the external cache dir SD card path * 
* 
private static String getexternalcached IR (Context context) { 
  //Android 2.2 features supported 
  if (Hasexternalcachedir ()) {return 
   Context.getexternalcachedir (). GetPath () + File.separator + "gesture"; 
  } 
  Before Froyo we need to construct the external cache dir ourselves 
  //2.2 Before we need to construct 
  the final String cachedir = " /android/data/"+ context.getpackagename () +"/cache/gesture/"; 
  Return Environment.getexternalstoragedirectory (). GetPath () + Cachedir; 
} 

3. Get the actual space size of the SDcard:

public static long Getrealsizeonsdcard () { 
  file path = new File (Environment.getexternalstoragedirectory ()). GetAbsolutePath ()); 
  Statfs stat = new Statfs (Path.getpath ()); 
  Long blockSize = Stat.getblocksize (); 
  Long availableblocks = Stat.getavailableblocks (); 
  return availableblocks * blockSize; 
} 

-----> Detect if there is enough room for SDcard:

/** 
* @param updatesize specified detection space size 
* @return true space enough to return true, insufficient return false/public 
Static Boolean Enoughspaceonsdcard (Long updatesize) { 
  String status = Environment.getexternalstoragestate (); 
  if (!status.equals (environment.media_mounted)) return 
   false; 
  Return (Updatesize < Getrealsizeonsdcard ()); 
} 

4. Get the storage size of the phone:

public static long Getrealsizeonphone () { 
  File path = Environment.getdatadirectory (); 
  Statfs stat = new Statfs (Path.getpath ()); 
  Long blockSize = Stat.getblocksize (); 
  Long availableblocks = Stat.getavailableblocks (); 
  Long realsize = blockSize * availableblocks; 
  return realsize; 
} 

----> Detect if the phone store has enough space:

/** 
* @param updatesize specified detection space size 
* @return space enough to return true, insufficient return false/public 
Static Boolean Enoughspaceonphone (Long updatesize) {return 
  getrealsizeonphone () > updatesize; 
} 

A little gift of memory that comes with a long time ago:

1. Add Local_certificate to Android.mk: = platform can use the System Hide API (@hide).

2.Activity Startup Mode Summary:

1). Standard:

Stack (Task): On the same stack as other activated activity of the application

Instance creation: A new instance is created each time it is started

2). Singletop:

Stack (Task): On the same stack as other activated activity of the application

Instance creation: At startup, check to see if an instance of the activity is on the current top of the stack (the record that was started). If so, the new instance is no longer created, and if not, recreate the new instance and place it on the top of the stack.

3). Singletask:

Stack (Task): On the same stack as other activated activity of the application

Instance creation: When starting, check to see if there are instances of the activity in the task. If so, all other activity instances on the task that are above the activity instance are stack (POPs),
Make it on top of the stack. If not, recreate the activity instance and place it on the top of the stack.

4). SingleInstance:

Stack (Task): Unlike the other three modes, a new task is created, placing the acitvity in the new task, and ensuring that no other activity instances are entered.

Instance creation: The first time you create a new task, it is created as a new task. If an instance already exists, it is not necessary to create a new instance and reuse the previously created instance at startup.

3. Set the background of the activity to the background of the mobile desktop:

Add GetWindow (). SetFlags before the Setcontentview method (WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER),

Then add the Android:theme= "@android: Style/theme.translucent" attribute in the Androidmanifest.xml file to enable the activity to be set to transparent.

4. View the phone memory space code Long Freememory = Runtime.getruntime (). Freememory ();

5. About Android Main thread:

The main thread in Android is the UI thread, which is the thread that operates on the UI component of Android, and the UI component operation in Android is not thread safe.

After all, the update operation of the UI component requires a quick response, and if the update considers thread safety, synchronization locks wait for the response, and so on, the update response of the UI component may be delayed,
This does not meet the ANR exception that Android requires to avoid as much as possible.

The 6.android4.4 system publishes a art runtime that is ready to replace the Dalvik virtual machine that has been used before, in the hope of resolving the much-maligned performance problem.

7. Reference Related:

Softreference<t>: Soft Reference--> when the virtual machine is out of memory, the object it points to is recycled, and the Get method can be called when the object needs to be fetched.

Weakreference<t>: Weak reference--> may be reclaimed by the garbage collector at any time.

SoftReference used to implement the cache mechanism, WeakReference is generally used to prevent memory leaks, to ensure that the memory is recycled by the VM.

8.TCP and UDP

Because a connection-oriented TCP protocol requires retransmission in the event of a packet loss, this

affect the real-time nature of the video. UDP because it is a transaction-oriented and simple and unreliable transport protocol,

In the transmission of video data has a fast, small consumption of resources, simple transmission process generated by

Packet loss and disorderly ordering can be handled at the video receiver end. Therefore, UDP protocol is generally used as the Transport Layer protocol for multimedia communication.

9. Memory Related:

1. The memory of a process can consist of 2 parts: Java uses memory, C uses memory,

These two memory and must be less than 16M (16M is how to come?) It is the experiment, each model is different, the simulator is not the same version, you can pass:
Runtime.getmaxmemory () to view. ), otherwise will appear everybody familiar oom, this is the first kind of oom situation.

2). What's even more strange is this: once the memory is allocated to Java, after the memory is released, it can only be used for Java, this estimate is related to the Java virtual machine in the memory divided into several blocks for caching, anyway C do not want to use this block of memory.

10. Get the bottom virtual button height (for mobile phones with no physical keys):

/** 
* Get bottom virtual button height (for mobile phone without physical button) 
* @return * * 
private int getnavigationbarheight () { 
  int ResourceID = Getresources (). Getidentifier ("Navigation_bar_height", "Dimen", "Android"); 
  if (ResourceID > 0) {return 
   getresources (). Getdimensionpixelsize (ResourceID); 
  } 
  return 0; 
}

I hope this article will help you with the Android program.

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.