android 获取信息和安装Apk

原创
2013/09/11 09:00
阅读数 414
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);
 }
}

 
 
 
 

  
  
  
展开阅读全文
加载中

作者的其它热门文章

0
1 收藏
分享
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部