本文共 16802 字,大约阅读时间需要 56 分钟。
一般下载完APK文件之后,都要打开软件安装页面,提示用户进行安装,可以用以下方法(固定写法)
1 2 3 4 5 | Intent intent = new Intent(); intent.setAction( "android.intent.action.VIEW" ); intent.addCategory( "android.intent.category.DEFAULT" ); intent.setDataAndType(Uri.fromFile( new File( "apk在手机中的路径/要安装的软件.apk" )), "application/vnd.android.package-archive" ); startActivityForResult(intent, 0 ); |
如果想要分享软件,需要写好预定要分享出去的信息,可用以下方法:
1 2 3 4 5 6 | Intent intent = new Intent(); intent.setAction( "android.intent.action.SEND" ); intent.addCategory( "android.intent.category.DEFAULT" ); intent.setType( "text/plain" ); intent.putExtra(Intent.EXTRA_TEXT, "(自定义内容)这是一个很牛逼的软件,你信不信" ); startActivity(intent); |
卸载软件一般都是使用Android内置的卸载工具来卸载的,只需要传入要卸载的软件的包名,便可以打开其卸载页面:
1 2 3 4 5 | Intent intent = new Intent(); intent.setAction( "android.intent.action.DELETE" ); intent.addCategory( "android.intent.category.DEFAULT" ); intent.setData(Uri.parse( "package:" + "卸载软件的包名" )); startActivityForResult(intent, 0 ); |
此方法打开Android内关于软件的详情页,需要传入软件报名
1 2 3 4 | Intent intent = new Intent(); intent.setAction( "android.settings.APPLICATION_DETAILS_SETTINGS" ); intent.setData(Uri.parse( "package:" + "要打开软件的包名" )); startActivity(intent); |
在自己的app中打开其他应用可以使用这个方法
1 2 3 4 5 | PackageManager manager = getPackageManager(); Intent launchIntentForPackage = manager.getLaunchIntentForPackage( "要打开软件的包名" ); if (launchIntentForPackage != null ) { startActivity(launchIntentForPackage); } |
1 2 3 4 5 | //跳转到主界面 Intent intent = new Intent(); intent.setAction( "android.intent.action.MAIN" ); intent.addCategory( "android.intent.category.HOME" ); startActivity(intent); |
注册开屏锁屏的广播的IntentFliter所要添加的action是
1 2 | Intent.ACTION_SCREEN_OFF // 锁屏时添加 Intent.ACTION_SCREEN_ON // 开屏是添加 |
获取app信息(报名、版本名、应用图标、应用名称、是用户程序还是系统程序、安装在SD卡中还是手机内存中)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | public class AppEnging { public static List<AppInfo> getAppInfos(Context context){ List<AppInfo> list = new ArrayList<AppInfo>(); //获取应用程序信息 //包的管理者 PackageManager pm = context.getPackageManager(); //获取系统中安装到所有软件信息 List<PackageInfo> installedPackages = pm.getInstalledPackages( 0 ); for (PackageInfo packageInfo : installedPackages) { //获取包名 String packageName = packageInfo.packageName; //获取版本号 String versionName = packageInfo.versionName; //获取application ApplicationInfo applicationInfo = packageInfo.applicationInfo; int uid = applicationInfo.uid; //获取应用程序的图标 Drawable icon = applicationInfo.loadIcon(pm); //获取应用程序的名称 String name = applicationInfo.loadLabel(pm).toString(); //是否是用户程序 //获取应用程序中相关信息,是否是系统程序和是否安装到SD卡 boolean isUser; int flags = applicationInfo.flags; if ((applicationInfo.FLAG_SYSTEM & flags) == applicationInfo.FLAG_SYSTEM) { //系统程序 isUser = false ; } else { //用户程序 isUser = true ; } //是否安装到SD卡 boolean isSD; if ((applicationInfo.FLAG_EXTERNAL_STORAGE & flags) == applicationInfo.FLAG_EXTERNAL_STORAGE) { //安装到了SD卡 isSD = true ; } else { //安装到手机中 isSD = false ; } //添加到bean中 AppInfo appInfo = new AppInfo(name, icon, packageName, versionName, isSD, isUser); //将bean存放到list集合 list.add(appInfo); } return list; } } // 封装软件信息的bean类 class AppInfo { //名称 private String name; //图标 private Drawable icon; //包名 private String packagName; //版本号 private String versionName; //是否安装到SD卡 private boolean isSD; //是否是用户程序 private boolean isUser; public AppInfo() { super (); } public AppInfo(String name, Drawable icon, String packagName, String versionName, boolean isSD, boolean isUser) { super (); this .name = name; this .icon = icon; this .packagName = packagName; this .versionName = versionName; this .isSD = isSD; this .isUser = isUser; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | public class TaskEnging { /** * 获取系统中所有进程信息 * @return */ public static List<TaskInfo> getTaskAllInfo(Context context){ List<TaskInfo> list = new ArrayList<TaskInfo>(); //1.进程的管理者 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); PackageManager pm = context.getPackageManager(); //2.获取所有正在运行的进程信息 List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses(); //遍历集合 for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { TaskInfo taskInfo = new TaskInfo(); //3.获取相应的信息 //获取进程的名称,获取包名 String packagName = runningAppProcessInfo.processName; taskInfo.setPackageName(packagName); //获取进程所占的内存空间,int[] pids : 输入几个进程的pid,就会返回几个进程所占的空间 MemoryInfo[] memoryInfo = activityManager.getProcessMemoryInfo( new int []{runningAppProcessInfo.pid}); int totalPss = memoryInfo[ 0 ].getTotalPss(); long ramSize = totalPss* 1024 ; taskInfo.setRamSize(ramSize); try { //获取application信息 //packageName : 包名 flags:指定信息标签 ApplicationInfo applicationInfo = pm.getApplicationInfo(packagName, 0 ); //获取图标 Drawable icon = applicationInfo.loadIcon(pm); taskInfo.setIcon(icon); //获取名称 String name = applicationInfo.loadLabel(pm).toString(); taskInfo.setName(name); //获取程序的所有标签信息,是否是系统程序是以标签的形式展示 int flags = applicationInfo.flags; boolean isUser; //判断是否是用户程序 if ((applicationInfo.FLAG_SYSTEM & flags) == applicationInfo.FLAG_SYSTEM) { //系统程序 isUser = false ; } else { //用户程序 isUser = true ; } //保存信息 taskInfo.setUser(isUser); } catch (NameNotFoundException e) { e.printStackTrace(); } //taskinfo添加到集合 list.add(taskInfo); } return list; } } class TaskInfo { //名称 private String name; //图标 private Drawable icon; //占用的内存空间 private long ramSize; //包名 private String packageName; //是否是用户程序 private boolean isUser; //checkbox是否被选中 private boolean isChecked = false ; public String getName() { return name; } public void setName(String name) { this .name = name; } public Drawable getIcon() { return icon; } public void setIcon(Drawable icon) { this .icon = icon; } public long getRamSize() { return ramSize; } public void setRamSize( long ramSize) { this .ramSize = ramSize; } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this .packageName = packageName; } public boolean isUser() { return isUser; } public void setUser( boolean isUser) { this .isUser = isUser; } public TaskInfo() { super (); // TODO Auto-generated constructor stub } public TaskInfo(String name, Drawable icon, long ramSize, String packageName, boolean isUser) { super (); this .name = name; this .icon = icon; this .ramSize = ramSize; this .packageName = packageName; this .isUser = isUser; } @Override public String toString() { return "TaskInfo [name=" + name + ", icon=" + icon + ", ramSize=" + ramSize + ", packageName=" + packageName + ", isUser=" + isUser + "]" ; } public boolean isChecked() { return isChecked; } public void setChecked( boolean isChecked) { this .isChecked = isChecked; } } |
1 2 3 4 5 6 7 8 9 | ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); //获取正在运行进程 List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses(); for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { //判断我们的应用进程不能被清理 if (!runningAppProcessInfo.processName.equals(getPackageName())) { am.killBackgroundProcesses(runningAppProcessInfo.processName); } } |
查看服务是否在后台开启,当用户直接停止服务的时候也能够检测得到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static boolean isRunningService(String className,Context context){ //进程的管理者,活动的管理者 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //获取正在运行的服务 List<RunningServiceInfo> runningServices = activityManager.getRunningServices( 1000 ); //maxNum 返回正在运行的服务的上限个数,最多返回多少个服务 //遍历集合 for (RunningServiceInfo runningServiceInfo : runningServices) { //获取控件的标示 ComponentName service = runningServiceInfo.service; //获取正在运行的服务的全类名 String className2 = service.getClassName(); //将获取到的正在运行的服务的全类名和传递过来的服务的全类名比较,一直表示服务正在运行 返回true,不一致表示服务没有运行 返回false if (className.equals(className2)) { return true ; } } return false ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** * 获取SD卡可用空间 */ public static long getAvailableSD(){ //获取SD卡路径 File path = Environment.getExternalStorageDirectory(); //硬盘的API操作 StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); //获取每块的大小 long totalBlocks = stat.getBlockCount(); //获取总块数 long availableBlocks = stat.getAvailableBlocks(); //获取可用的块数 return availableBlocks*blockSize; } /** *获取内存可用空间 * @return */ public static long getAvailableROM(){ //获取内存路径 File path = Environment.getDataDirectory(); //硬盘的API操作 StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); //获取每块的大小 long totalBlocks = stat.getBlockCount(); //获取总块数 long availableBlocks = stat.getAvailableBlocks(); //获取可用的块数 return availableBlocks*blockSize; } |
进行简单的MD5加密,传入一个String,输出经过MD5加密后的密码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public static String passwordMD5(String password){ StringBuilder sb = new StringBuilder(); try { //1.获取数据摘要器 //arg0 : 加密的方式 MessageDigest messageDigest = MessageDigest.getInstance( "MD5" ); //2.将一个byte数组进行加密,返回的是一个加密过的byte数组,二进制的哈希计算,md5加密的第一步 byte [] digest = messageDigest.digest(password.getBytes()); //3.遍历byte数组 for ( int i = 0 ; i < digest.length; i++) { //4.MD5加密 //byte值 -128-127 int result = digest[i] & 0xff ; //将得到int类型转化成16进制字符串 //String hexString = Integer.toHexString(result)+1;//不规则加密,加盐 String hexString = Integer.toHexString(result); if (hexString.length() < 2 ) { // System.out.print("0"); sb.append( "0" ); } //System.out.println(hexString); //e10adc3949ba59abbe56e057f20f883e sb.append(hexString); } return sb.toString(); } catch (NoSuchAlgorithmException e) { //找不到加密方式的异常 e.printStackTrace(); } return null ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /** * 获取剩余内存 * @return */ public static long getAvailableRam(Context context){ ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //获取内存的信息,保存到memoryinfo中 MemoryInfo outInfo = new MemoryInfo(); am.getMemoryInfo(outInfo); //获取空闲的内存 //outInfo.availMem; // //获取总的内存 // outInfo.totalMem; return outInfo.availMem; } /** * 获取总的内存 * @return * @deprecated */ public static long getTotalRam(Context context){ ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //获取内存的信息,保存到memoryinfo中 MemoryInfo outInfo = new MemoryInfo(); am.getMemoryInfo(outInfo); //获取空闲的内存 //outInfo.availMem; // //获取总的内存 // outInfo.totalMem; return outInfo.totalMem; //16版本之上才有,之下是没有的 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * 根据手机的分辨率从 dip 的单位 转成为 px(像素) * @return */ public static int dip2qx(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; //获取屏幕的密度 return ( int ) (dpValue * scale + 0 .5f); //+0.5f四舍五入 3.7 3 3.7+0.5 = 4.2 4 } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return ( int ) (pxValue / scale + 0 .5f); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | public static List<HashMap<String, String>> getAllContactInfo(Context context) { SystemClock.sleep( 3000 ); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); // 1.获取内容解析者 ContentResolver resolver = context.getContentResolver(); // 2.获取内容提供者的地址:com.android.contacts // raw_contacts表的地址 :raw_contacts // view_data表的地址 : data // 3.生成查询地址 Uri raw_uri = Uri.parse( "" ); Uri date_uri = Uri.parse( "" ); // 4.查询操作,先查询raw_contacts,查询contact_id // projection : 查询的字段 Cursor cursor = resolver.query(raw_uri, new String[] { "contact_id" }, null , null , null ); // 5.解析cursor while (cursor.moveToNext()) { // 6.获取查询的数据 String contact_id = cursor.getString( 0 ); // cursor.getString(cursor.getColumnIndex("contact_id"));//getColumnIndex // : 查询字段在cursor中索引值,一般都是用在查询字段比较多的时候 // 判断contact_id是否为空 if (!TextUtils.isEmpty(contact_id)) { //null "" // 7.根据contact_id查询view_data表中的数据 // selection : 查询条件 // selectionArgs :查询条件的参数 // sortOrder : 排序 // 空指针: 1.null.方法 2.参数为null Cursor c = resolver.query(date_uri, new String[] { "data1" , "mimetype" }, "raw_contact_id=?" , new String[] { contact_id }, null ); HashMap<String, String> map = new HashMap<String, String>(); // 8.解析c while (c.moveToNext()) { // 9.获取数据 String data1 = c.getString( 0 ); String mimetype = c.getString( 1 ); // 10.根据类型去判断获取的data1数据并保存 if (mimetype.equals( "vnd.android.cursor.item/phone_v2" )) { // 电话 map.put( "phone" , data1); } else if (mimetype.equals( "vnd.android.cursor.item/name" )) { // 姓名 map.put( "name" , data1); } } // 11.添加到集合中数据 list.add(map); // 12.关闭cursor c.close(); } } // 12.关闭cursor cursor.close(); return list; } |
使用startActivityForResult()开启Activity,然后再onActivityResult()方法中获取返回的联系人号码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // 在按钮点击事件中设置Intent, Intent intent = new Intent(); intent.setAction( "android.intent.action.PICK" ); intent.addCategory( "android.intent.category.DEFAULT" ); intent.setType( "vnd.android.cursor.dir/phone_v2" ); startActivityForResult(intent, 1 ); @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { super .onActivityResult(requestCode, resultCode, data); if (data != null ){ Uri uri = data.getData(); String num = null ; // 创建内容解析者 ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query(uri, null , null , null , null ); while (cursor.moveToNext()){ num = cursor.getString(cursor.getColumnIndex( "data1" )); } cursor.close(); num = num.replaceAll( "-" , "" ); //替换的操作,555-6 -> 5556 } } |
获取手机中的短信信息,并保存为XML文件,路径为:/mnt/sdcard/backupsms.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | /** * 获取短信 */ public static void getAllSMS(Context context){ //1.获取短信 //1.1获取内容解析者 ContentResolver resolver = context.getContentResolver(); //1.2获取内容提供者地址 sms,sms表的地址:null 不写 //1.3获取查询路径 Uri uri = Uri.parse( "" ); //1.4.查询操作 //projection : 查询的字段 //selection : 查询的条件 //selectionArgs : 查询条件的参数 //sortOrder : 排序 Cursor cursor = resolver.query(uri, new String[]{ "address" , "date" , "type" , "body" }, null , null , null ); //设置最大进度 int count = cursor.getCount(); //获取短信的个数 //2.备份短信 //2.1获取xml序列器 XmlSerializer xmlSerializer = Xml.newSerializer(); try { //2.2设置xml文件保存的路径 //os : 保存的位置 //encoding : 编码格式 xmlSerializer.setOutput( new FileOutputStream( new File( "/mnt/sdcard/backupsms.xml" )), "utf-8" ); //2.3设置头信息 //standalone : 是否独立保存 xmlSerializer.startDocument( "utf-8" , true ); //2.4设置根标签 xmlSerializer.startTag( null , "smss" ); //1.5.解析cursor while (cursor.moveToNext()){ SystemClock.sleep( 1000 ); //2.5设置短信的标签 xmlSerializer.startTag( null , "sms" ); //2.6设置文本内容的标签 xmlSerializer.startTag( null , "address" ); String address = cursor.getString( 0 ); //2.7设置文本内容 xmlSerializer.text(address); xmlSerializer.endTag( null , "address" ); xmlSerializer.startTag( null , "date" ); String date = cursor.getString( 1 ); xmlSerializer.text(date); xmlSerializer.endTag( null , "date" ); xmlSerializer.startTag( null , "type" ); String type = cursor.getString( 2 ); xmlSerializer.text(type); xmlSerializer.endTag( null , "type" ); xmlSerializer.startTag( null , "body" ); String body = cursor.getString( 3 ); xmlSerializer.text(body); xmlSerializer.endTag( null , "body" ); xmlSerializer.endTag( null , "sms" ); System.out.println( "address:" +address+ " date:" +date+ " type:" +type+ " body:" +body); } xmlSerializer.endTag( null , "smss" ); xmlSerializer.endDocument(); //2.8将数据刷新到文件中 xmlSerializer.flush(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
1 2 3 4 5 6 7 | // 获取屏幕的宽度 WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); // windowManager.getDefaultDisplay().getWidth(); DisplayMetrics outMetrics = new DisplayMetrics(); // 创建了一张白纸 windowManager.getDefaultDisplay().getMetrics(outMetrics); // 给白纸设置宽高 width = outMetrics.widthPixels; height = outMetrics.heightPixels; |
转载地址:http://trzpi.baihongyu.com/