Home > database >  Android Asyntask API deprecated
Android Asyntask API deprecated

Time:09-28

Been looking on the internet how to check the current app version of the application and the version available on Play Store. But it was using Asynctask that was deprecated already and Ive been looking alternative way aside from Asynctask been searching on the internet but I couldnt figure out how to do it correctly. Please check the code below:

private class GetLatestVersion extends AsyncTask<String, String, String> {
    private String latestVersion;
    private ProgressDialog progressDialog;
    private boolean manualCheck;
    GetLatestVersion(boolean manualCheck) {
        this.manualCheck = manualCheck;
    }
    
        String currentVersion = getCurrentVersion();
        //If the versions are not the same
        if(!currentVersion.equals(latestVersion)&&latestVersion!=null){
            final Dialog dialog = new Dialog(activity);
            dialog.setContentView(R.layout.custom_warning_dialog);
            dialog.setCancelable(false);
            Button tryAgain = dialog.findViewById(R.id.bt_positive);
            Button settings = dialog.findViewById(R.id.bt_negative);
            tryAgain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" activity.getPackageName())));
                    dialog.dismiss();
                }
            });
            settings.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.finish();
                }
            });
            dialog.show();
        }else {
            if (manualCheck) {
                Toast.makeText(activity, "No Update Available", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    @Override
    protected String doInBackground(String... params) {
        try {
            //It retrieves the latest version by scraping the content of current version from play store at runtime
            latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id="   activity.getPackageName()   "&hl=en")
                    .timeout(30000)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .get()
                    .select(".hAyfc .htlgb")
                    .get(7)
                    .ownText();
            return latestVersion;
        } catch (Exception e) {
            return latestVersion;
        }
    }
}

CodePudding user response:

Here's how you can basically convert asyncTask to coroutine on kotlin

private suspend fun GetLatestVersion(String,String,String) {

   withContext(Dispatcher.Default)
   {
        // you just have to refactor all your code inside doInBaakground inside this withContext
   }

   //then if you want to update the UI
   withContext(Dispatchers.Main) 
   {
      //return code for your task
   }
}

See this link . For further information.

CodePudding user response:

Thanks Ginxx, but I used executer instead of coroutine since I am only a beginner but it might help others who the same as me.

Check my update codes below:

public void CheckLatestAppVersionFromPlayStore() {
    final String[] latestAppVersionFromPlayStore = new String[1];
    ExecutorService executor = Executors.newSingleThreadExecutor();
    final Handler handler = new Handler(Looper.getMainLooper());

    executor.execute(new Runnable() {
        @Override
        public void run() {
            try {
                //It retrieves the latest version by scraping the content of current version from play store at runtime
                latestAppVersionFromPlayStore[0] = Jsoup.connect("https://play.google.com/store/apps/details?id="   activityWeakReference.get().getPackageName()   "&hl=it")
                        .timeout(30000)
                        .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                        .referrer("http://www.google.com")
                        .get()
                        .select(".hAyfc .htlgb")
                        .get(7)
                        .ownText();
            } catch (Exception e) {
                e.printStackTrace();
            }

            handler.post(new Runnable() {
                @Override
                public void run() {
                    final String installedAppVersion = currentVersionName;
                    //If the versions are not the same
                    if (!installedAppVersion.equals(latestAppVersionFromPlayStore[0]))
                        if (latestAppVersionFromPlayStore[0] != null) {
                            final Dialog dialog = new Dialog(activityWeakReference.get());
                            dialog.setContentView(R.layout.custom_warning_dialog);
                            dialog.setCancelable(false);
                            Button tryAgain = dialog.findViewById(R.id.bt_positive);
                            Button settings = dialog.findViewById(R.id.bt_negative);
                            tryAgain.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    activityWeakReference.get().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="   activityWeakReference.get().getPackageName())));
                                }
                            });
                            settings.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    activityWeakReference.get().finish();
                                }
                            });
                            // to avoid the bad token exception
                            if ((activityWeakReference.get() != null) && (!activityWeakReference.get().isFinishing()) && (!activityWeakReference.get().isDestroyed()))
                                dialog.show();
                        }
                }
            });
        }
    });
}

For further information please click this link.

  • Related