因为要在一款Android系统的产品上实现APK的自动升级功能,所以搜索了网上关于这方面的资料:http://blog.csdn.net/liufuliang163/article/details/73770589
在上述资料的基础上,结合我们的目标系统Android4.4.2,研究了一下相关代码
通过两种方式实现的需求,修改默认界面方式和不带界面方式
修改默认界面方式:
PackageInstallerActivity.java
因为是activity,首先看 onCreate 方法,
第一步先获得 intent 携带的信息,
// get intent information
final Intent intent = getIntent();
mPackageURI = intent.getData();
mOriginatingURI = intent.getParcelableExtra(Intent.EXTRA_ORIGINATING_URI);
mReferrerURI = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
mPm = getPackageManager();
第二步,安装过程解析 mInstallFlowAnalytics = new InstallFlowAnalytics();
注意这步 boolean requestFromUnknownSource = isInstallRequestFromUnknownSource(intent); 为了能够安装未知来源的APP,此方法做了修改
第三步,intent 携带的参数检查
第四步,设置显示界面
//set view
setContentView(R.layout.install_start);
mInstallConfirm = findViewById(R.id.install_confirm_panel);
mInstallConfirm.setVisibility(View.INVISIBLE);
PackageUtil.initSnippetForNewApp(this, as, R.id.app_snippet);
第五步,初始化安装信息 initiateInstall();
我们改造的目标是吧由用户选择的地方都由程序自动完成,主要修改下面提到的几个方法,具体请参考代码资源
startInstallConfirm -> appInstall
InstallAppProgress->mHandler->handleMessage
不带界面方式:
主要添加了两个类 PackageInstallerHideActivity UninstallerHideActivity
PackageInstallerHideActivity 静默安装
// lfl 20170623 add
class PackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
// Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
// msg.arg1 = returnCode;
// mHandler.sendMessage(msg);
Intent result = new Intent();
result.putExtra(Intent.EXTRA_INSTALL_RESULT, returnCode);
setResult(returnCode == PackageManager.INSTALL_SUCCEEDED ? Activity.RESULT_OK: Activity.RESULT_FIRST_USER, result);
finish();
}
}
//lfl 20170623 add start
private void installAPK()
{
int installFlags = 0;
String packageName = mPkgInfo.packageName;
PackageManager pm = getPackageManager();
try
{
PackageInfo pi = pm.getPackageInfo(packageName,PackageManager.GET_UNINSTALLED_PACKAGES);
if(pi != null)
{
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
}
}
catch (NameNotFoundException e)
{}
String installerPackageName = getIntent().getStringExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME);
PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(mPackageURI, observer, installFlags,installerPackageName);
}
//lfl 20170623 add end
UninstallerHideActivity 静默卸载APP
在oncrete 方法中添加
//lfl add 20170626
if(mAppInfo == null || errFlag) {
Log.e(TAG, "Invalid packageName or componentName in " + packageURI.toString());
setResult(Activity.RESULT_FIRST_USER);
finish();
} else {
PackageDeleteObserver observer = new PackageDeleteObserver();
getPackageManager().deletePackage(mAppInfo.packageName, observer, 0);
}
//lfl add 20170626
添加内部类
//lfl add 20170626
class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
public void packageDeleted(String packageName, int returnCode) {
Intent result = new Intent();
result.putExtra(Intent.EXTRA_INSTALL_RESULT, returnCode);
setResult(
returnCode == PackageManager.DELETE_SUCCEEDED ? Activity.RESULT_OK
: Activity.RESULT_FIRST_USER, result);
finish();
}
}
相关代码:http://download.csdn.net/detail/liufuliang163/9881675