前言
終于又攢了一篇出來,本系列以實用為主,歡迎和我分享和推薦好用的代碼段~~
聲明
歡迎轉載,但請保留文章原始出處:)
部落格園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com
正文
一、擷取已經安裝apk的路徑
packagemanager pm = getpackagemanager();
for (applicationinfo app : pm.getinstalledapplications(0)) {
log.d("packagelist", "package: " + app.packagename + ", sourcedir: " + app.sourcedir);
}
輸出如下:
package: com.tmobile.thememanager, sourcedir: /system/app/thememanager.apk
package: com.touchtype.swiftkey, sourcedir: /data/app/com.touchtype.swiftkey-1.apk
二、 多程序preferences資料共享
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) {
return sharedpreferences.getstring(key, defvalue);
@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()));
private void savecurrentposition() {
if (mlistview != null) {
int position = mlistview.getfirstvisibleposition();
view v = mlistview.getchildat(0);
int top = (v == null) ? 0 : v.gettop();
//儲存position和top
private void restoreposition() {
if (mfolder != null && mlistview != null) {
int position = 0;//取出儲存的資料
int top = 0;//取出儲存的資料
mlistview.setselectionfromtop(position, top);
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;
public static string getip(context ctx) {
return formatter.formatipaddress((wifimanager) ctx.getsystemservice(context.wifi_service).getconnectioninfo().getipaddress());
public static void sortfiles(file[] files) {
arrays.sort(files, new comparator<file>() {
@override
public int compare(file lhs, file rhs) {
//傳回負數表示o1 小于o2,傳回0 表示o1和o2相等,傳回正數表示o1大于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());
}
}
});