获取手机安装包信息+运行应用信息
1 PacInfo pacInfo=new PacInfo(); 2 ArrayList<AppInfo> appInfos=pacInfo.Get(SysInfoService.this); 3 StringBuffer stringBuffer=new StringBuffer(); 4 stringBuffer.append("\n\n"+"日记时间:"+FileStore.date()); 5 stringBuffer.append("\n"+"*************安装的包信息*************"+"\n"); 6 int i=0; 7 for(AppInfo appInfo:appInfos) 8 { 9 String s=++i+". 应用名:"+appInfo.getAppname()+" 包名:"+appInfo.getPackagename()+" 包安装路径:"+appInfo.getInstalPath()+" 最后安装日期:"+new SimpleDateFormat("yy/MM/dd HH:mm").format(appInfo.getLastInstal())+" 应用版本号:"+appInfo.getVersionCode()+"\n"; 10 stringBuffer.append(s); 11 } 12 stringBuffer.append("*************************************"+"\n"); 13 /** 14 *安装的三方包信息 15 */ 16 ArrayList<AppInfo> appInfos3rd=pacInfo.GetOnly3rd(SysInfoService.this); 17 StringBuffer stringBuffer3rd=new StringBuffer(); 18 stringBuffer3rd.append("\n\n"+"日记时间:"+FileStore.date()); 19 stringBuffer3rd.append("\n"+"*************安装的三方包信息*************"+"\n"); 20 int i3rd=0; 21 for(AppInfo appInfo:appInfos3rd) 22 { 23 String s=++i3rd+". 应用名:"+appInfo.getAppname()+" 包名:"+appInfo.getPackagename()+" 包安装路径:"+appInfo.getInstalPath()+" 最后安装日期:"+new SimpleDateFormat("yy/MM/dd HH:mm").format(appInfo.getLastInstal())+" 应用版本号:"+appInfo.getVersionCode()+"\n"; 24 stringBuffer3rd.append(s); 25 } 26 stringBuffer3rd.append("**********************************************"+"\n"); 27 /** 28 * 正在运行的应用信息 29 */ 30 List<RunningAppInfo> appInfosRunning=pacInfo.queryAllRunningAppInfo(SysInfoService.this); 31 StringBuffer stringBufferRun=new StringBuffer(); 32 stringBufferRun.append("\n\n"+"日记时间:"+FileStore.date()); 33 stringBufferRun.append("\n"+"*************正在运行的应用信息*************"+"\n"); 34 int iRun=0; 35 for(RunningAppInfo appInfo:appInfosRunning) 36 { 37 String s=++iRun+" 应用:"+appInfo.getAppLabel()+" 包名:"+appInfo.getPkgName()+" 应用PID:"+appInfo.getPid()+" 应用进程名:"+appInfo.getProcessName()+"\n"; 38 stringBufferRun.append(s); 39 } 40 stringBufferRun.append("*************************************"+"\n"); 41 42 // FileStore.write(stringBuffer.toString(),FileStore.getLogFile()); 43 FileWriter writer = null; 44 try { 45 writer = new FileWriter(FileStore.getLogFile(), true); 46 writer.write(stringBuffer.toString()+"\n"); 47 writer.write(stringBuffer3rd.toString()+"\n"); 48 writer.write(stringBufferRun.toString()+"\n"); 49 writer.close(); 50 } catch (IOException e) { 51 e.printStackTrace(); 52 } 53 stringBuffer=null;
文件存储类
1 package com.alipay.store; 2 3 import android.os.Environment; 4 5 import java.io.File; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 import java.text.SimpleDateFormat; 9 import java.util.Date; 10 11 /** 12 * Created by xianyu.hxy on 2015/6/8. 13 */ 14 public class FileStore { 15 public static final String CACHE_DIR_NAME = "Alipay"; 16 /** 17 * 以年月日作为日志文件名称 18 * @return 19 */ 20 public static String date() 21 { 22 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System 23 .currentTimeMillis())); 24 } 25 26 /** 27 * 保存到日志文件 28 * @param content 29 */ 30 public static synchronized void write(String content) 31 { 32 try 33 { 34 FileWriter writer = new FileWriter(getFile(), true); 35 writer.write(content+"\n"); 36 writer.close(); 37 } 38 catch (IOException e) 39 { 40 e.printStackTrace(); 41 } 42 } 43 public static synchronized void write(String content,String path) 44 { 45 try 46 { 47 FileWriter writer = new FileWriter(getFile(path), true); 48 writer.write(content+"\n"); 49 writer.close(); 50 } 51 catch (IOException e) 52 { 53 e.printStackTrace(); 54 } 55 } 56 57 /** 58 * 获取日志文件路径 59 * @return 60 */ 61 public static String getFile() 62 { 63 File sdDir = null; 64 65 if (Environment.getExternalStorageState().equals( 66 android.os.Environment.MEDIA_MOUNTED)) 67 sdDir = Environment.getExternalStorageDirectory(); 68 69 File cacheDir = new File(sdDir + File.separator + CACHE_DIR_NAME); 70 71 if (!cacheDir.exists()) 72 cacheDir.mkdir(); 73 74 File filePath = new File(cacheDir + File.separator + date() + ".txt"); 75 76 return filePath.toString(); 77 } 78 // path :/storage/emulated/0/Alipay/log.txt 79 public static String getLogFile() 80 { 81 File sdDir = null; 82 83 if (Environment.getExternalStorageState().equals( 84 android.os.Environment.MEDIA_MOUNTED)) 85 sdDir = Environment.getExternalStorageDirectory(); 86 87 File cacheDir = new File(sdDir + File.separator + CACHE_DIR_NAME); 88 89 if (!cacheDir.exists()) 90 cacheDir.mkdir(); 91 92 File filePath = new File(cacheDir + File.separator + "log.txt"); 93 94 95 return filePath.toString(); 96 } 97 98 public static File getLogFile(Boolean b) 99 { 100 File sdDir = null; 101 102 if (Environment.getExternalStorageState().equals( 103 android.os.Environment.MEDIA_MOUNTED)) 104 sdDir = Environment.getExternalStorageDirectory(); 105 106 File cacheDir = new File(sdDir + File.separator + CACHE_DIR_NAME); 107 108 if (!cacheDir.exists()) 109 cacheDir.mkdir(); 110 111 File filePath = new File(cacheDir + File.separator + "log.txt", String.valueOf(true)); 112 113 114 return filePath; 115 } 116 public static String getFile(String Path) 117 { 118 File sdDir = null; 119 120 if (Environment.getExternalStorageState().equals( 121 android.os.Environment.MEDIA_MOUNTED)) 122 sdDir = Environment.getExternalStorageDirectory(); 123 124 File cacheDir = new File(sdDir + File.separator + CACHE_DIR_NAME+File.separator+Path); 125 126 if (!cacheDir.exists()) 127 cacheDir.mkdir(); 128 129 File filePath = new File(cacheDir + File.separator + date() + ".txt"); 130 131 return filePath.toString(); 132 } 133 134 }
用到的存储类
package com.alipay.info; import android.content.pm.ProviderInfo; import android.graphics.drawable.Drawable; import android.util.Log; /** * Created by xianyu.hxy on 2015/6/2. */ public class AppInfo { private int versionCode = 0; //版本号 private String appname = "";// 程序名称 private String packagename = ""; //包名称 private Drawable appicon = null;//图标 private long lastInstal;//最后一次安装时间 private ProviderInfo[] provider;//供应商 private String InstalPath;//安装路径 public int getVersionCode() { return versionCode; } public void setVersionCode(int versionCode) { this.versionCode = versionCode; } public String getAppname() { return appname; } public void setAppname(String appname) { this.appname = appname; } public String getPackagename() { return packagename; } public void setPackagename(String packagename) { this.packagename = packagename; } public Drawable getAppicon() { return appicon; } public void setAppicon(Drawable appicon) { this.appicon = appicon; } /** * @return the lastInstal */ public long getLastInstal() { return lastInstal; } /** * @param firstInstallTime the lastInstal to set */ public void setLastInstal(long firstInstallTime) { this.lastInstal = firstInstallTime; } /** * @return the provider */ public ProviderInfo[] getProvider() { return provider; } /** * @param providers the provider to set */ public void setProvider(ProviderInfo[] providers) { this.provider = providers; } /** * @return the instalPath */ public String getInstalPath() { return InstalPath; } /** * @param instalPath the instalPath to set */ public void setInstalPath(String instalPath) { InstalPath = instalPath; } }
1 package com.alipay.info; 2 3 import android.graphics.drawable.Drawable; 4 5 /** 6 * Created by xianyu.hxy on 2015/6/8. 7 */ 8 9 //Model类 ,用来存储应用程序信息 10 public class RunningAppInfo { 11 12 private String appLabel; //应用程序标签 13 private Drawable appIcon ; //应用程序图像 14 private String pkgName ; //应用程序所对应的包名 15 16 private int pid ; //该应用程序所在的进程号 17 private String processName ; // 该应用程序所在的进程名 18 19 public RunningAppInfo(){} 20 21 public String getAppLabel() { 22 return appLabel; 23 } 24 public void setAppLabel(String appName) { 25 this.appLabel = appName; 26 } 27 public Drawable getAppIcon() { 28 return appIcon; 29 } 30 public void setAppIcon(Drawable appIcon) { 31 this.appIcon = appIcon; 32 } 33 public String getPkgName(){ 34 return pkgName ; 35 } 36 public void setPkgName(String pkgName){ 37 this.pkgName=pkgName ; 38 } 39 40 public int getPid() { 41 return pid; 42 } 43 44 public void setPid(int pid) { 45 this.pid = pid; 46 } 47 48 public String getProcessName() { 49 return processName; 50 } 51 52 public void setProcessName(String processName) { 53 this.processName = processName; 54 } 55 56 57 }
将一些安装包信息存到本地,方式可扩展.