I've made a service to download an updated version of my application when it exists. I would like to display a message on the screen advising that there is an update and install / update behind my application. It is possible? Thank you in advance.
I've made a service to download an updated version of my application when it exists. I would like to display a message on the screen advising that there is an update and install / update behind my application. It is possible? Thank you in advance.
Installing the application automatically in the background I think it is not possible because the device would need to be rooted. But you can show a screen asking if the person wants to download the update and let it decide whether to install or not, follow the commented code:
I'm considering that you're already bringing in a service information saying you have an update, so if you have just use the following code. In my case I used DialogFragment
to show the screen saying there is an update, but this is at your discretion:
private static String file_url = "";
private String appName = "";
private File file;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button = (Button) ...;
file_url = "url do seu arquivo .apk";
appName = "nome do seu aplicativo (não esqueça de colocar .apk no final)";
file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + appName));
if (file.exists()) { // Se o arquivo já existe (estou verifcando na pasta download)
abreArquivo();
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) }
new DownloadAtualizacao().execute(file_url);
}
});
}
// Essa classe irá fazer o download do apk em background
class DownloadAtualizacao extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Faça algo como mostrar uma ProgressBar antes de começar a baixar
}
@Override
protected String doInBackground(String... f_url) {
int count;
String erro = "N";
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int tamFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 10 * 1024);
OutputStream output = new FileOutputStream(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + appName));
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / tamFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) { erro = "S"; }
return erro;
}
protected void onProgressUpdate(String... progress) {
// utilize para incrementar o progresso da ProgressBar
// use assim progressBar.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String erro) {
if (erro.equals("S")) {
// Faça algo aqui se ocorreu algum erro
}
else {
abreArquivo();
}
}
}
private void abreArquivo() {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + appName);
String type = getType(file);
Intent it = new Intent();
it.setAction(Intent.ACTION_VIEW);
it.setDataAndType(Uri.fromFile(file), type);
try { startActivity(it); } catch (Exception e) { e.printStackTrace(); }
}
// Pega o mymeType do arquivo(pode ser usado para qualquer tipo de arquivo)
private String getType(File file) {
String type = null;
try {
URL u = file.toURL();
URLConnection uc = null;
uc = u.openConnection();
type = uc.getContentType();
} catch (Exception e) { e.printStackTrace(); }
return type;
}
You can do this as follows:
Service :
Your Service can tell the app that there are new updates!
Through JSON
(for example) you can tell which version is the most current.
You can also prevent a user from using a very old version: JSON Example:
{“LAST_VERSION”: “3.7",
“ALLOWED_WITH_UPDATE” :” 3.6",
“NOT_ALLOWED”: “2.8”}
Being:
LAST_VERSION : the latest version;
ALLOWED_WITH_UPDATE : There are updates, but the user does not need to update;
NOT_ALLOWED : User necessarily needs to update;
Android :
Every time the app starts, it performs this query and informs the User if there are any updates (via Dialog
, for example).
If the version is less than or equal to NOT_ALLOWED , Dialog
only allows updating (crashing the app's features).
To find out which app version you use, use the following Constant: BuildConfig.VERSION_NAME
You do not have to do anything specific for this, update notification is Google Play's responsibility.
You just have to upload your new apk and change the versionCode
of it in the manifest.
Edit
There are other possibilities like using a hockeyApp or Fabric that they have a system that as soon as the user opens the app it opens a dialog stating that an update is available.