I know there is many stack-overflow posts for this question, but i couldn't solve my problem using any of them.
My app is only available for a limited group of people(inner company app), so i can't publish it in an app store and i want it to be updated automatically when i release a new version on the app server.
it is a device admin
app with no root permission. i found this approach to achieve this goal but it didn't work.
I have this permission in my AndroidManifest.xml
file to install packages:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
This is my code:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void installPackage(Context context, String filePath, String packageName)
throws IOException
{
Log.d(TAG, "installPackage: start");
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(packageName);
// set params
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
//
InputStream in = new FileInputStream(new File(filePath));
OutputStream out = session.openWrite(packageName, 0, -1);
IOUtils.copyStream(in, out);
//
session.fsync(out);
in.close();
out.close();
//
session.commit(PendingIntent.getActivity(context, 112233, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT).getIntentSender());
Log.d(TAG, "installPackage: commit");
}
And this is onCreate
method of my MainActivity
class:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ComponentName componentName = new ComponentName(this, AdminReceiver.class);
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (getIntent() != null && dpm != null)
{
Log.d(TAG, "onCreate: is app admin : " + dpm.isAdminActive(componentName));
Log.d(TAG, "onCreate: status : " + getIntent().getIntExtra("android.content.pm.extra.STATUS", 1000));
Log.d(TAG, "onCreate: intent : " + getIntent().getParcelableExtra("android.intent.extra.INTENT"));
}
}
and this is my logcat
result :
2019-07-08 15:03:24.147 5841-5841/com.test.learncheshmak D/AutoUpdateHelper: downloadUpdate: SUCCESSFUL
2019-07-08 15:03:24.147 5841-5841/com.test.learncheshmak D/AutoUpdateHelper: installPackage: start
2019-07-08 15:03:24.295 5841-5841/com.test.learncheshmak D/AutoUpdateHelper: installPackage: commit
2019-07-08 15:03:24.298 5841-5895/com.test.learncheshmak V/FA: Recording user engagement, ms: 11740
2019-07-08 15:03:24.302 5841-5841/com.test.learncheshmak W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@fe51cb8
2019-07-08 15:03:24.307 5841-5841/com.test.learncheshmak V/FA: onActivityCreated
2019-07-08 15:03:24.308 5841-5895/com.test.learncheshmak V/FA: Connecting to remote service
2019-07-08 15:03:24.308 5841-5841/com.test.learncheshmak D/MainActivity: onCreate: is app admin : true
2019-07-08 15:03:24.309 5841-5841/com.test.learncheshmak D/MainActivity: onCreate: status : -1
2019-07-08 15:03:24.309 5841-5841/com.test.learncheshmak D/MainActivity: onCreate: intent : Intent { act=android.content.pm.action.CONFIRM_PERMISSIONS pkg=com.google.android.packageinstaller (has extras) }
Here, as we see, status is -1. according to this document i got STATUS_PENDING_USER_ACTION
and it determine we need user prompt to update our app.but this app is a device admin(device owner) app and according to this document:
Committing may require user intervention to complete the installation, unless the caller falls into one of the following categories, in which case the installation will complete automatically.
- the device owner
- the affiliated profile owner
so if app be device owner
install progress will perform automatically (with out user prompt). then why do i get STATUS_PENDING_USER_ACTION
while my app is a device admin
app?