本文共 4854 字,大约阅读时间需要 16 分钟。
package com.example.yokindemo;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity"; private Button jia, uninst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); copyApkFromAssets(this, "jia.apk", Environment .getExternalStorageDirectory().getAbsolutePath() + "/jia.apk"); jia = (Button) findViewById(R.id.jia); jia.setOnClickListener(this); uninst = (Button) findViewById(R.id.button1); uninst.setOnClickListener(this); } public boolean install(String apkPath) { boolean result = false; DataOutputStream dataOutputStream = null; BufferedReader errorStream = null; try { Process process = Runtime.getRuntime().exec("su"); dataOutputStream = new DataOutputStream(process.getOutputStream()); String argString = "pm install -r " + apkPath + "\n"; String command = new String(argString.getBytes(),"UTF-8"); dataOutputStream.write(command.getBytes()); dataOutputStream.flush(); dataOutputStream.writeBytes("exit\n"); dataOutputStream.flush(); process.waitFor(); errorStream = new BufferedReader(new InputStreamReader( process.getErrorStream())); String msg = ""; String line; while ((line = errorStream.readLine()) != null) msg += line; if (!msg.contains("Failure")) result = true; } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } finally { try { if (dataOutputStream != null) dataOutputStream.close(); if (errorStream != null) errorStream.close(); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } } return result; } private int getAPKVersion(){ String filePath = Environment .getExternalStorageDirectory().getAbsolutePath() + "/jia.apk"; PackageManager packageManager = getPackageManager(); PackageInfo packageInfo = packageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES); return packageInfo.versionCode; } private void uninstallAPK(){ Uri packageURI = Uri.parse("package:com.example.jia"); Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); startActivity(uninstallIntent); } private boolean isAppInstalled(String packName) { PackageManager pm = getPackageManager(); boolean installed = false; try { Log.d(TAG, "apk="+getAPKVersion()); PackageInfo pi = pm.getPackageInfo(packName, PackageManager.GET_ACTIVITIES); Log.d(TAG, "ver="+pi.versionCode); installed = true; } catch (PackageManager.NameNotFoundException e) { installed = false; } return installed; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.jia: if (isAppInstalled("com.example.jia")) { Toast.makeText(MainActivity.this, "jia已经安装", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "jia还没安装", Toast.LENGTH_LONG).show(); } Intent intent = new Intent(); ComponentName cn = new ComponentName("com.example.jia", "com.example.jia.MainActivity"); intent.setComponent(cn); intent.setAction("android.intent.action.MAIN"); try { startActivityForResult(intent, RESULT_OK); } catch (Exception e) { if (!install(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/jia.apk")) { Intent intent1 = new Intent(Intent.ACTION_VIEW); intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent1.setDataAndType( Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/jia.apk"), "application/vnd.android.package-archive"); startActivity(intent1); } } break; case R.id.button1: Log.d(TAG, "button1"); uninstallAPK(); break; default: break; } } public boolean copyApkFromAssets(Context context, String fileName, String path) { boolean copyIsFinish = false; try { InputStream is = context.getAssets().open(fileName); File file = new File(path); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); byte[] temp = new byte[1024]; int i = 0; while ((i = is.read(temp)) > 0) { fos.write(temp, 0, i); } fos.close(); is.close(); copyIsFinish = true; } catch (IOException e) { e.printStackTrace(); } return copyIsFinish; }}
转载地址:http://lanub.baihongyu.com/