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());
                }
            }
        });
    }

 

 

系列 

Android實用代碼七段(二) 

Android實用代碼七段(一) 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM