I am working on an app for my company's internal use which will collect performance stats from network and post them on our Grafana server. The app works fine with this context, but there is a problem: App will run on a phone at a datacenter and it will be very difficult to access it if we need to update the app for adding features. Also the phone will not have internet access. So I won't be able to update the app manually , or using Google Play. I thought of writing a function to check a static URL and when we put an updated apk there, it would download it and install.
I wrote this class (copying from another Stackoverflow question):
class updateApp extends AsyncTask<String, Void, String> {
protected String doInBackground(String... sUrl) {
File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS),"updates");
if(!file.mkdir()){
}
File f = new File(file.getAbsolutePath(),"YourApp.apk");
Uri fUri = FileProvider.getUriForFile(MainActivity.this,"com.aktuna.vtv.monitor.fileprovider",f);
String path = fUri.getPath();
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(path);
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("YourApp", "Well that didn't work out so well...");
Log.e("YourApp", e.getMessage());
}
return path;
}
@Override
protected void onPostExecute(String path) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS),"updates");
File f = new File(file.getAbsolutePath(),"YourApp.apk");
Uri fUri = FileProvider.getUriForFile(MainActivity.this,"com.aktuna.vtv.monitor.fileprovider",f);
i.setDataAndType(fUri, "application/vnd.android.package-archive" );
myCtx.startActivity(i);
}
}
It seems to download the file successfully. And then it sends the file in the intent to the installer (I can see this because the packageinstaller selection prompt comes) But then it does not install the new apk.
Since the previous Stackoverflow question is 7 years old, I thought that updating with no user interaction may be forbidden in new API levels.
But I am not sure. How can I troubleshoot this further ?
Also, I am open to any suggestions to achieve this, maybe something making use of older API levels or anything that would solve the "updating with no internet access through an internal static URL" issue.
Thanks.