需求:在没有操作屏的设备上,通过手机连接设备发送命令可以触发应用升级,但升级过程中需要不断点击弹出的确认、安装、允许等命令,需默认允许所有弹窗、使手机发送命令后整个安装过程能自动完成并自动重新打开升级后的应用。
原理
Android 默认的安装应用的系统应用为 PackageInstaller,首先确定平台源码没有裁剪该应用,然后修改该应用 onResume 时的相关处理。自动重新打开升级后的应用有专门的一个类 InstallSuccess
专门负责后续操作。具体原理流程待研究、具体修改方法如下。
修改方法
// packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
....
public class PackageInstallerActivity extends OverlayTouchActivity implements OnClickListener {
...
@Override
protected void onResume() {
super.onResume();
if (mOK != null) {
mOk.setEnabled(mEnableOk);
}
// Added by Enoch : application upgrade sliently
if (mOkCanInstall || mScrollView == null) {
if (mSessionId != -1) {
mInstaller.setPermissionsResult(mSessionId, true);
finish();
}
} else {
mScrollView.pageScroll(View.FOCUS_DOWN);
}
// Added end.
}
...
}
....
// packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallSuccess.java
...
public class InstallSuccess extends Activity {
...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)) {
...
} else {
...
// Enable or disable "launch" button
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(
appInfo.packageName);
boolean enabled = false;
if (launchIntent != null) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(launchIntent,
0);
if (list != null && list.size() > 0) {
enabled = true;
}
}
Button launchButton = (Button)findViewById(R.id.launch_button);
if (enabled) {
launchButton.setOnClickListener(view -> {
try {
startActivity(launchIntent);
} catch (ActivityNotFoundException | SecurityException e) {
Log.e(LOG_TAG, "Could not start activity", e);
}
finish();
});
// Added by Enoch : autolaunch application after upgrading
try {
startActivity(launchIntent);
} catch (ActivityNotFoundException | SecurityException e) {
Log.e(LOG_TAG, "Could not start activity", e);
}
finish();
// Added end.
} else {
launchButton.setEnabled(false);
}
}
...
}