I have been trying to update application from apk. I have tried in different ways (signing the apk and updating the app from the apk in downloads). Nothing was successful. Can anyone suggest a way to do this?
You can look at my code below where I am copying the apk file form the assets folder to internal storage and invoke the apk as an update for that application.
public class UpdateApp extends AsyncTask<String,Void,Void> {
private Context context;
public UpdateApp(Context context) {
this.context=context;
}
@Override
protected Void doInBackground(String... arg0) {
String PATH = getFilesDir()+"/Download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "App.apk");
if(outputFile.exists()){
outputFile.delete();
//Toast.makeText(MainActivity.this,"wfejhjbweff",Toast.LENGTH_LONG).show();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
AssetManager assetManager = getAssets();
// Toast.makeText(MainActivity.this,""+s,Toast.LENGTH_LONG).show();
try {
InputStream in = assetManager.open("App.apk");
if(in==null){
}
try{
OutputStream out = fos;
byte[] buffer = new byte[1024];
int read = in.read(buffer);
while (read != -1) {
out.write(buffer, 0, read);
read = in.read(buffer);
}
}
catch (Exception e){
}
OpenNewVersion(PATH);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
void OpenNewVersion(String location) {
try{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(location + "App.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
catch (Exception e){
}
}
}