Android Utility code seven paragraph (iii)

Source: Internet
Author: User

Objective

Finally saved an article, this series to practical, welcome and I share and recommend the use of code snippets ~ ~

Statement
Welcome reprint, but please keep the original source of the article:)
Blog Park: http://www.cnblogs.com
Farmer Uncle: Http://over140.cnblogs.com

Body

First, get the path where the APK has been installed

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/thememanager.apk
Package:com.touchtype.swiftkey, SourceDir:/data/app/com.touchtype.swiftkey-1.apk

reproduced from here.

Second, 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. 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 Recovery ListView Current Location

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;//Remove the saved data
int top = 0;//to remove saved data
Mlistview.setselectionfromtop (position, top);
}
}

can be saved in preference or in a database, and then set after the data is loaded.

V. Calling 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;
}

Six, formatted output IP address

public static String GetIP (Context ctx) {
Return formatter.formatipaddress ((Wifimanager) Ctx.getsystemservice (context.wifi_service). GetConnectionInfo (). Getipaddress ());
}

Seven, folder sorting (first folder sorting, after file sorting)

public static void Sortfiles (file[] files) {
Arrays.sort (Files, new comparator<file> () {

@Override
public int Compare (file lhs, file rhs) {
A negative number indicates that O1 is less than O2, returning 0 means O1 and O2 are equal, and returning positive numbers means 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 ());
}
}
});
}

Android Utility code seven paragraph (iii)

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.