博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android实用代码七段(三)
阅读量:4554 次
发布时间:2019-06-08

本文共 2664 字,大约阅读时间需要 8 分钟。

 

前言

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

 

声明

欢迎转载,但请保留文章原始出处:) 
博客园: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);
    }

 相关文章:

 

三、泛型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());
                }
            }
        });
    }

 

 

系列 

 

 

 

转载于:https://www.cnblogs.com/firecode/p/3145105.html

你可能感兴趣的文章
20165301 2017-2018-2 《Java程序设计》第四周学习总结
查看>>
Vue的简单入门
查看>>
urllib 中的异常处理
查看>>
通过SQL Server的扩展事件来跟踪SQL语句在运行时,时间都消耗到哪儿了?
查看>>
SQL优化:重新编译存储过程和表
查看>>
PCB“有铅”工艺将何去何从?
查看>>
Solr环境搭建
查看>>
垂直居中的几种实现方法
查看>>
UILabel标签文字过长时的显示方式
查看>>
H5离线缓存机制-manifest
查看>>
比较:I/O成员函数getline() 与 get()(第二种用法)的用法异同
查看>>
201671010118 2016-2017-2《Java程序设计》 第十一周学习心得
查看>>
Get Sauce(状压DP)
查看>>
Office2007 升级到 office2010
查看>>
SpringBoot整合Hibernate
查看>>
PPT1 例2
查看>>
extern外部方法使用C#简单例子
查看>>
血液循环结构
查看>>
SQL Server统计数据库中表个数、视图个数、存储过程个数
查看>>
设计模式:观察者模式
查看>>