前言

终于又攒了一篇出来,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~

声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com

正文

一、获取已经安装APK的路径

PackageManagerpm=getPackageManager();

for(ApplicationInfoapp: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 voidputStringProcess(Contextctx,Stringkey,Stringvalue){
SharedPreferencessharedPreferences=ctx.getSharedPreferences("preference_mu",Context.MODE_MULTI_PROCESS);
Editoreditor=sharedPreferences.edit();
editor.putString(key,value);
editor.commit();
}

public staticStringgetStringProcess(Contextctx,Stringkey,StringdefValue){
SharedPreferencessharedPreferences=ctx.getSharedPreferences("preference_mu",Context.MODE_MULTI_PROCESS);
returnsharedPreferences.getString(key,defValue);
}

相关文章:

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

三、泛型ArrayList转数组

@SuppressWarnings("unchecked")
public static<T>T[]toArray(Class<?>cls,ArrayList<T>items){
if(items== null||items.size()==0){
return(T[])Array.newInstance(cls,0);
}
returnitems.toArray((T[])Array.newInstance(cls,items.size()));
}

四、 保存恢复ListView当前位置

private voidsaveCurrentPosition(){
if(mListView!= null){
intposition=mListView.getFirstVisiblePosition();
Viewv=mListView.getChildAt(0);
inttop=(v== null)?0:v.getTop();
// 保存position和top
}
}

private voidrestorePosition(){
if(mFolder!= null&&mListView!= null){
intposition=0; // 取出保存的数据
inttop=0; // 取出保存的数据
mListView.setSelectionFromTop(position,top);
}
}

可以保存在Preference中或者是数据库中,数据加载完后再设置。

五、调用便携式热点和数据共享设置

public staticIntentgetHotspotSetting(){
Intentintent= newIntent();
intent.setAction(Intent.ACTION_MAIN);
ComponentNamecom= newComponentName("com.android.settings","com.android.settings.TetherSettings");
intent.setComponent(com);
returnintent;
}

六、 格式化输出IP地址

public staticStringgetIp(Contextctx){
returnFormatter.formatIpAddress((WifiManager)ctx.getSystemService(Context.WIFI_SERVICE).getConnectionInfo().getIpAddress());
}

七、 文件夹排序(先文件夹排序,后文件排序)

public static voidsortFiles(File[]files){
Arrays.sort(files, newComparator<File>(){

@Override
public intcompare(Filelhs,Filerhs){
// 返回负数表示o1小于o2,返回0表示o1和o2相等,返回正数表示o1大于o2。
booleanl1=lhs.isDirectory();
booleanl2=rhs.isDirectory();
if(l1&&!l2)
return-1;
else if(!l1&&l2)
return1;
else{
returnlhs.getName().compareTo(rhs.getName());
}
}
});
}

系列

Android实用代码七段(二)

Android实用代码七段(一)

更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. Android(安卓)activity 开启时数据保存和恢复
  3. Android(安卓)Jetpack架构组件-Paging介绍及实践
  4. Android实现简单的分页效果
  5. android的存储GreenDao数据库
  6. Android(安卓)修改Camera拍照的默认保存路径
  7. AndroidStudio练习用--登录Demo
  8. Android(安卓)| 1. Fragment中使用SQLite实现简单存储及查询
  9. Android(安卓)LsitView的实现

随机推荐

  1. Android 收缩展开动画
  2. android launcher语言设置修改
  3. Android 桌面未读
  4. android volley
  5. android service 之 Binder
  6. android异步发网络请求
  7. Android UI 设计准则
  8. Android头部视差效果的实现方式
  9. CookieManager
  10. android 基本工程配置