android 获取信息和安装Apk

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值