博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 获取信息和安装Apk
阅读量:5900 次
发布时间:2019-06-19

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

  hot3.png

package com.hes.tools;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import android.content.Context;import android.content.Intent;import android.content.pm.ApplicationInfo;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.net.Uri;import android.os.Environment;import android.telephony.TelephonyManager;public class PhoneInfo {	private static PhoneInfo info;	private Context mContext;	private TelephonyManager tm;	private PackageManager pm;	private PhoneInfo(Context context) {		this.mContext = context;		tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);		pm = context.getPackageManager();	}	public static PhoneInfo getPhoneInfo(Context context) {		if (info == null) {			info = new PhoneInfo(context);		}		return info;	}	public String getIMEI() {		String imei = null;		imei = tm.getDeviceId();		return imei;	}	public String getIMSI() {		String imsi = null;		imsi = tm.getSubscriberId();		return imsi;	}		public String getApkPackageName(String path) {		PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);		ApplicationInfo appInfo = null;		if (info != null) {			appInfo = info.applicationInfo;			String packageName = appInfo.packageName;			return packageName;		}		return null;	}	public void installAPK(String apkAbsolutePath) {		Intent intent = new Intent("android.intent.action.VIEW");		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);		intent.setDataAndType(Uri.fromFile(new File(apkAbsolutePath)), "application/vnd.android.package-archive");		mContext.startActivity(intent);	}	public void installAPK(File file) {		Intent intent = new Intent("android.intent.action.VIEW");		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);		intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");		mContext.startActivity(intent);	}	public void uninstallAPK(String packageName) {		Uri packageURI = Uri.parse("package:" + packageName);		Intent uninstallIntent = new Intent("android.intent.action.DELETE", packageURI);		uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);		mContext.startActivity(uninstallIntent);	}	public String installApk(String apkAbsolutePath) {		String[] args = { "pm", "install", "-r", apkAbsolutePath };		String result = "";		ProcessBuilder processBuilder = new ProcessBuilder(args);		Process process = null;		InputStream errIs = null;		InputStream inIs = null;		try {			ByteArrayOutputStream baos = new ByteArrayOutputStream();			int read = -1;			process = processBuilder.start();			errIs = process.getErrorStream();			while ((read = errIs.read()) != -1) {				baos.write(read);			}			baos.write(10);			inIs = process.getInputStream();			while ((read = inIs.read()) != -1) {				baos.write(read);			}			byte[] data = baos.toByteArray();			result = new String(data);		} catch (IOException e) {			e.printStackTrace();		} catch (Exception e) {			e.printStackTrace();		} finally {			try {				if (errIs != null) {					errIs.close();				}				if (inIs != null)					inIs.close();			} catch (IOException e) {				e.printStackTrace();			}			if (process != null) {				process.destroy();			}		}		return result;	}	public String uninstallApk(String packageName) {		String[] args = { "pm", "uninstall", "-k", packageName };		String result = "";		ProcessBuilder processBuilder = new ProcessBuilder(args);		Process process = null;		InputStream errIs = null;		InputStream inIs = null;		try {			ByteArrayOutputStream baos = new ByteArrayOutputStream();			int read = -1;			process = processBuilder.start();			errIs = process.getErrorStream();			while ((read = errIs.read()) != -1) {				baos.write(read);			}			baos.write(10);			inIs = process.getInputStream();			while ((read = inIs.read()) != -1) {				baos.write(read);			}			byte[] data = baos.toByteArray();			result = new String(data);		} catch (IOException e) {			e.printStackTrace();		} catch (Exception e) {			e.printStackTrace();		} finally {			try {				if (errIs != null) {					errIs.close();				}				if (inIs != null)					inIs.close();			} catch (IOException e) {				e.printStackTrace();			}			if (process != null) {				process.destroy();			}		}		return result;	}	public boolean isSDCard() {		boolean isSDCard = false;		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {			isSDCard = true;		}		return isSDCard;	}}
package com.hes.tools;import java.io.InputStream;import java.util.PropertyResourceBundle;public class ResourceSetting { private static ResourceSetting rs; private static PropertyResourceBundle resourceBundle; public static ResourceSetting getResourceSetting(String settingFileName) { if (rs == null) { rs = new ResourceSetting(); } if (resourceBundle == null) { resourceBundle = (PropertyResourceBundle) PropertyResourceBundle.getBundle(settingFileName); } return rs; } public static String getProperty(String key) { return resourceBundle.getString(key); } public InputStream getResourceStream(String packageFile) { return getClass().getResourceAsStream(packageFile); }}

转载于:https://my.oschina.net/hes/blog/160541

你可能感兴趣的文章
php的春天,swoole处理高并发
查看>>
python实现命令行交互
查看>>
【前端面试】字节跳动2019校招面经 - 前端开发岗(三)
查看>>
从重复到重用
查看>>
错误消息 This computer doesn't have VT-X/AMD-v enabled
查看>>
小程序外卖项目实践之-左右菜单联动
查看>>
java多线程系列:通过对战游戏学习CyclicBarrier
查看>>
递归查询级联信息
查看>>
Yoshino: 一个基于React的可定制化的PC组件库
查看>>
二叉树
查看>>
Redis+Lua实现限流
查看>>
Array 的三个技巧
查看>>
Vue学习笔记第一天--知识点
查看>>
Spring Cloud Security OAuth2(二):搭建资源服务端
查看>>
jQuery动画效果、jQuery插件使用
查看>>
MIT 黑科技:通过脑电波和手势控制机器人
查看>>
Github博客搭建(1)
查看>>
Vue入坑教程(一)——搭建vue-cli脚手架
查看>>
php环境篇:linux编译安装php7.2
查看>>
mysql解决乱码问题
查看>>