Android 6.0 静默安装控制
Posted 王睿丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 6.0 静默安装控制相关的知识,希望对你有一定的参考价值。
framework层
diff --git a/packages/apps/PackageInstaller/res/values/styles.xml b/packages/apps/PackageInstaller/res/values/styles.xml
index 7f6a4693c2..9b7ba25d25 100755
--- a/packages/apps/PackageInstaller/res/values/styles.xml
+++ b/packages/apps/PackageInstaller/res/values/styles.xml
@@ -16,6 +16,10 @@
<resources>
+ <style name="NoDisplay"
+ parent="@android:style/Theme.NoDisplay">
+ </style>
<style name="Theme.DialogWhenLarge"
parent="@android:style/Theme.DeviceDefault.Light.DialogWhenLarge.NoActionBar">
</style>
diff --git a/packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallAppProgress.java b/packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallAppProgress.java
index d51cab1dd2..6233d8b050 100755
--- a/packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallAppProgress.java
+++ b/packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallAppProgress.java
@@ -72,6 +72,8 @@ public class InstallAppProgress extends Activity implements View.OnClickListener
private Intent mLaunchIntent;
private static final int DLG_OUT_OF_SPACE = 1;
private CharSequence mLabel;
+ private boolean mSilentInstall = false;
private Handler mHandler = new Handler()
public void handleMessage(Message msg)
@@ -109,6 +111,11 @@ public class InstallAppProgress extends Activity implements View.OnClickListener
enabled = true;
+ // chenxs add begin
+ if (mSilentInstall)
+ finish();
+
+ // chenxs add end
if (enabled)
mLaunchButton.setOnClickListener(InstallAppProgress.this);
else
@@ -178,9 +185,23 @@ public class InstallAppProgress extends Activity implements View.OnClickListener
InstallFlowAnalytics.RESULT_FAILED_UNSUPPORTED_SCHEME);
throw new IllegalArgumentException("unexpected scheme " + scheme);
+ android.util.Log.d("wangrui","222");
+ mSilentInstall = intent.getBooleanExtra("silent_install", false);
+ Log.i("chenxs","[InstallAppProgress] onCreate mSilentInstall=" + mSilentInstall);
+ if (mSilentInstall)
+ setTheme(R.style.NoDisplay);
+
initView();
+
+ @Override
+ protected void onResume()
+ super.onResume();
+ if (mSilentInstall)
+ finish();
+
+
@Override
public Dialog onCreateDialog(int id, Bundle bundle)
diff --git a/packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java b/packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
old mode 100644
new mode 100755
index 6bcd80e41d..e4081a6b5d
--- a/packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
+++ b/packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
@@ -74,6 +74,8 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen
private Uri mReferrerURI;
private int mOriginatingUid = VerificationParams.NO_UID;
private ManifestDigest mPkgDigest;
+
+ private boolean mSilentInstall = false;
private boolean localLOGV = false;
PackageManager mPm;
@@ -438,12 +440,12 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen
@Override
protected void onCreate(Bundle icicle)
super.onCreate(icicle);
-
mPm = getPackageManager();
mInstaller = mPm.getPackageInstaller();
mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
final Intent intent = getIntent();
+ mSilentInstall = intent.getIntExtra("ISSILENT", 0)==1;
if (PackageInstaller.ACTION_CONFIRM_PERMISSIONS.equals(intent.getAction()))
final int sessionId = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, -1);
final PackageInstaller.SessionInfo info = mInstaller.getSessionInfo(sessionId);
@@ -544,7 +546,7 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen
initiateInstall();
return;
-
+ android.util.Log.d("wangrui","111");
// If the admin prohibits it, or we're running in a managed profile, just show error
// and exit. Otherwise show an option to take the user to Settings to change the setting.
final boolean isManagedProfile = mUserManager.isManagedProfile();
@@ -558,6 +560,9 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen
showDialogInner(DLG_UNKNOWN_SOURCES);
mInstallFlowAnalytics.setFlowFinished(
InstallFlowAnalytics.RESULT_BLOCKED_BY_UNKNOWN_SOURCES_SETTING);
+ else if(mSilentInstall)
+ mInstallFlowAnalytics.setInstallButtonClicked();
+ startInstall();
else
initiateInstall();
@@ -683,6 +688,7 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen
private void startInstall()
// Start subactivity to actually install the application
Intent newIntent = new Intent();
+ newIntent.putExtra("silent_install", mSilentInstall);
newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO,
mPkgInfo.applicationInfo);
newIntent.setData(mPackageURI);
App层
res/xml/fileproviderpath.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root" path="."></root-path>
<files-path
name="file"
path="."></files-path>
<files-path
name="files"
path="."></files-path>
<cache-path
name="cache"
path="."></cache-path>
<external-path
name="external"
path="."></external-path>
<external-cache-path
name="external_cache"
path="."></external-cache-path>
<external-files-path
name="external_file"
path="."></external-files-path>
<external-path name="external_files" path="."/>
</paths>
AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.silentinstallation.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/fileproviderpath" />
</provider>
启动安装
String path = "/sdcard/txsp.apk";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_VIEW);
//静默时传值,正常情况下不需要传
intent.putExtra("ISSILENT",1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
uri = FileProvider.getUriForFile(this,"com.example.silentinstallation.fileprovider",new File(path));
intent.setDataAndType(uri,"application/vnd.android.package-archive");
else
uri = Uri.parse("file://" + path);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
开发者涨薪指南
48位大咖的思考法则、工作方式、逻辑体系
以上是关于Android 6.0 静默安装控制的主要内容,如果未能解决你的问题,请参考以下文章