import java.io.File; public class UpdataActivity extends AppCompatActivity { public static final String PATH = Environment.getExternalStorageDirectory() + 大专栏Android File.separator;
public static final String NEW_APK_PATH = PATH + "new.apk";
public static final String PATCH_PATH = PATH + "test.patch";
static { System.loadLibrary("ApkPatchLibrary"); }
private FloatingActionButton mBut; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: Toast.makeText(UpdataActivity.this, "OK", Toast.LENGTH_SHORT) .show(); installApk();
break;
case -1: Toast.makeText(UpdataActivity.this, "error", Toast.LENGTH_SHORT).show();
this.mBut = (FloatingActionButton) findViewById(R.id.fab); this.mBut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { File patchFile = new File(PATCH_PATH); if (!patchFile.exists()) { Toast.makeText(UpdataActivity.this, "patch not exists", Toast.LENGTH_SHORT).show(); } else { new PatchThread().start(); } } }); }
private void installApk() { File file = new File(NEW_APK_PATH);
if (file.exists()) { Uri uri = Uri.fromFile(file); Intent installIntent = new Intent(Intent.ACTION_VIEW); installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); installIntent.setDataAndType(uri, "application/vnd.android.package-archive"); this.startActivity(installIntent); } }
class PatchThread extends Thread { @Override public void run() { String oldAppPatch = getApplicationContext().getApplicationInfo().sourceDir;
int patchResult = patch(oldAppPatch, NEW_APK_PATH, PATCH_PATH);