So in my app, I want to implement an auto-upgrade feature so that my app's users can upgrade the app easily. In order to do that I decided to use my syncAdapter (already usable for updating local SQLite from a remote mySQL Server). So in my syncAdapter I am making the call for my AsyncTask class that does the app-upgrade, like this:
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
this.context = mContext;
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority,ContentProviderClient provider, SyncResult syncResult) {
Context mcontext = mContext;
new AsyncGetVersiune(mcontext).execute("3", "1");
...
}
Then my AsyncGetVersiune class does this (if the upgrade is necessary):
public class AsyncGetVersiune extends AsyncTask<String, Integer, Double> {
private Context mContext;
public AsyncGetVersiune (Context context){
super();
mContext = context;
}
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
mContext.registerReceiver(downloadReceiver, filter);
final String calex = calea;
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("There is a newer version available. Upgrade now?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//if the user agrees to upgrade
public void onClick(DialogInterface dialog, int id) {
//start downloading the file using the download manager
downloadManager = (DownloadManager) mContext.getSystemService(mContext.DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(calex);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
request.setTitle("Download new version");
request.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, "Newversion.apk");
downloadReference = downloadManager.enqueue(request);
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.create().show();
But I get an error regarding the DialogBuilder. Apparently it does not like my Context...
*"Error parsing data android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"*
What can I do? I would like to have the Dialog inplace... So syncAdapter checks for new version using an AsyncTask class. If the AsyncTask finds new version, then in onPostExecute I want to display the dialog and from there... start download and install of the new version
But since I am starting the upgrade process from an syncAdapter... I cant have an Activity entangled here...
So what should I do?