Android实用代码七段(三)

简介:

前言

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

 

声明
欢迎转载,但请保留文章原始出处:) 
博客园: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) {
        SharedPreferences sharedPreferences = ctx.getSharedPreferences("preference_mu", Context.MODE_MULTI_PROCESS);
         return sharedPreferences.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);
        }
         return items.toArray((T[]) Array.newInstance(cls, items.size()));
    }
复制代码

 

四、 保存恢复ListView当前位置

复制代码
     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);
        }
    }
复制代码

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

 

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

复制代码
     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;
    }
复制代码

 

六、 格式化输出IP地址

     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());
                }
            }
        });
    }
复制代码

 

转载:http://www.cnblogs.com/over140/archive/2013/05/20/2948239.html

目录
相关文章
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
3月前
|
人工智能 IDE 开发工具
Studio Bot - 让 AI 帮我写 Android 代码
Studio Bot - 让 AI 帮我写 Android 代码
152 1
|
9月前
|
IDE 数据可视化 Java
Android自动生成代码,可视化脚手架之基础信息配置
今天的内容比较简单,大致过一下Electron一些基本用法,虽然说这些比较简单,但又是不得不去了解的,正如做Android的我们,也不是一上来就会的,需要一个循序渐进的过程,下一章,我们再去实际的开发功能。
129 0
|
2月前
|
Ubuntu 网络协议 Linux
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
48 0
|
3月前
|
安全 算法 JavaScript
安卓逆向 -- 关键代码定位与分析技术
安卓逆向 -- 关键代码定位与分析技术
39 0
|
3月前
|
安全 Android开发 数据安全/隐私保护
代码安全之代码混淆及加固(Android)
代码安全之代码混淆及加固(Android)
40 0
|
4月前
|
安全 Java Android开发
Android App开发之安全加固中反编译、代码混淆、第三方加固以及重签名的讲解及实战(图文解释 简单易懂)
Android App开发之安全加固中反编译、代码混淆、第三方加固以及重签名的讲解及实战(图文解释 简单易懂)
70 0
|
6月前
|
XML Java Android开发
Android 解决使用CocosCreator开发产品上架应用市场代码重复问题
Android 解决使用CocosCreator开发产品上架应用市场代码重复问题
271 0
|
7月前
|
JSON dexposed Java
一文总结 Android 隐私合规代码思路
一文总结 Android 隐私合规代码思路
|
7月前
|
SQL 程序员 Android开发
一行代码,利用 android studio自带的 liveTemplate 快速生成单例模式,程序员偷懒神器
一行代码,利用 android studio自带的 liveTemplate 快速生成单例模式,程序员偷懒神器