【定制Android系统】Android O 应用静默升级安装、安装后静默打开

需求:在没有操作屏的设备上,通过手机连接设备发送命令可以触发应用升级,但升级过程中需要不断点击弹出的确认、安装、允许等命令,需默认允许所有弹窗、使手机发送命令后整个安装过程能自动完成并自动重新打开升级后的应用。

原理

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);
		}
	}
	...
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深海Enoch

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值