Android code segment 7 (3)

Source: Internet
Author: User

Preface

Finally, I collected another article. This series focuses on practicality. Welcome to share with me and recommend useful code segments ~~

 

Statement
You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com
Farmer's uncle: http://over140.cnblogs.com

 

Body

1. Obtain the path of the installed APK

PackageManager pm = getPackageManager ();

For (ApplicationInfo app: pm. getInstalledApplications (0 )){
Log. d ("PackageList", "package:" + app. packageName + ", sourceDir:" + app. sourceDir );
}

The output is as follows:

Package: com. tmobile. thememanager, sourceDir:/system/app/themanager.apk
Package: com. touchtype. swiftkey, sourceDir:/data/apps/com.touchtype.swiftkey-1.apk

Reprinted from here.

 

2. multi-process Preferences data sharing

Public static void putStringProcess (Context ctx, String key, String value ){
SharedPreferences sharedPreferences = ctx. getSharedPreferences ("preference_mu", Context. MODE_MULTI_PROCESS );
Editor editor = sharedPreferences. edit ();
Editor. putString (key, value );
Editor. commit ();
}

Public static String getStringProcess (Context ctx, String key, String defValue ){
SharedPreferences sharedPreferences = ctx. getSharedPreferences ("preference_mu", Context. MODE_MULTI_PROCESS );
Return sharedPreferences. getString (key, defValue );
}

Related Articles:

Http://zengrong.net/post/1687.htm

 

Iii. Conversion from generic ArrayList to array

@ SuppressWarnings ("unchecked ")
Public static <T> T [] toArray (Class <?> Cls, ArrayList <T> items ){
If (items = null | items. size () = 0 ){
Return (T []) Array. newInstance (cls, 0 );
}
Return items. toArray (T []) Array. newInstance (cls, items. size ()));
}

 

Iv. Save and restore the current position of ListView

Private void saveCurrentPosition (){
If (mListView! = Null ){
Int position = mListView. getFirstVisiblePosition ();
View v = mListView. getChildAt (0 );
Int top = (v = null )? 0: v. getTop ();
// Save position and top
}
}

Private void restorePosition (){
If (mFolder! = Null & mListView! = Null ){
Int position = 0; // retrieves the stored data
Int top = 0; // retrieves the stored data
MListView. setSelectionFromTop (position, top );
}
}

It can be stored in Preference or in a database, and set after data is loaded.

 

5. Call portable hotspot and data sharing settings

Public static Intent getHotspotSetting (){
Intent intent = new Intent ();
Intent. setAction (Intent. ACTION_MAIN );
ComponentName com = new ComponentName ("com. android. settings", "com. android. settings. TetherSettings ");
Intent. setComponent (com );
Return intent;
}

 

6. format the output IP Address

Public static String getIp (Context ctx ){
Return Formatter. formatIpAddress (WifiManager) ctx. getSystemService (Context. WIFI_SERVICE). getConnectionInfo (). getIpAddress ());
}

 

7. Sort folders (sort folders first and sort files later)

 

Public static void sortFiles (File [] files ){
Arrays. sort (files, new Comparator <File> (){

@ Override
Public int compare (File lhs, File rhs ){
// Return a negative number indicating that o1 is less than o2, return 0 indicating that o1 is equal to o2, and return a positive number indicating that o1 is greater than o2.
Boolean l1 = lhs. isDirectory ();
Boolean l2 = rhs. isDirectory ();
If (l1 &&! L2)
Return-1;
Else if (! L1 & l2)
Return 1;
Else {
Return lhs. getName (). compareTo (rhs. getName ());
}
}
});
}

 

 

Series 

Android code segment 7 (II)

Android code segment 7 (1)

 

 

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.