Some Android devices call AsyncTask twice

Asked 2 years ago, Updated 2 years ago, 45 views

When I ran the program on Nexus 5, AsyncTask was called only once, but this time when I ran the program on Xperia, AsyncTask was called twice.Is there a bug in AsyncTask that says that the behavior will go wrong if the terminals are different?The source code is listed below.When I took the log, I found that doInBackground() in the PostTwitter class and doInBackground() in the TwitterOAuth class were called twice.In other words, you post twice on Twitter.Is there something wrong with using AsyncTask?I'm sorry, but I appreciate your cooperation.

PostTwitter class:

public class PostTwitter extensions AsyncTask<String, Void, Boolean>{
    private Activity mActivity;
    private Twitter mTwitter;

    public PostTwitter (Activity activity) {
        mActivity = activity;
        mTwitter=TwitterUtils.getTwitterInstance(activity.getApplicationContext());
    }

    public void shareTwitter(){
        if(TwitterUtils.hasAccessToken(mActivity.getApplicationContext())){
            share(mActivity);
        } else{
            TwitterOAuth twitterOAuth = new TwitterOAuth(mActivity);
            twitterOAuth.startAuthorize();
        }
    }

    @ Override
    protected Boolean doInBackground (String...params) {

        try{
            File file=new File(Environment.getExternalStorageDirectory().getPath()+"/image.png");
            StatusUpdate status = new StatusUpdate(params[0]); {
                status.setMedia(file);
            }
            mTwitter.updateStatus(status);
            return true;
        } catch(TwitterExceptione){
            e.printStackTrace();
            return false;
        }
    }

    @ Override
    protected void onPostExecute(Boolean result){
        if(result){
            showToast("Tweet Completed!");
        } else{
            showToast("Tweet failed...");
        }
    }

    private void showToast (String text) {
        Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
    }

    public void share (activity activity) {
        PostTwitter task = new PostTwitter(activity);
        task.execute(TwitterContents.getInstance().getmMessage());
    }
}

TwitterOAuthclasses:

public class TwitterOAuth{
    private Activity mActivity;
    private String mCallbackURL;
    private Twitter mTwitter;
    private RequestToken mRequestToken;
    private String mOAuthURL;
    private String mOAuthVerifier;

    public TwitterOAuth (Activity activity) {
        mActivity = activity;
        mCallbackURL=activity.getString(R.string.twitter_callback_url);
        mTwitter=TwitterUtils.getTwitterInstance(activity);
    }

    public void startAuthorize(){
        AsyncTask<Void,Void,String>task=newAsyncTask<Void,Void,String>(){
            @ Override
            Protected String doInBackground (Void...params) {
                try{
                    mRequestToken=mTwitter.getOAuthRequestToken(mCallbackURL);
                    mOAuthURL = mRequestToken.getAuthorizationURL();
                    return mOAuthURL;
                } catch(TwitterExceptione){
                    e.printStackTrace();
                }

                return null;
            }

            @ Override
            protected void onPostExecute(String url){
                if(url!=null){
                    showTwitterDialog();
                } else{
                    // Failed...
                }
            }
        };
        task.execute();
    }

    private void getAccessToken() {
        AsyncTask<String, Void, AccessToken>task=new AsyncTask<String, Void, AccessToken>(){
            @ Override
            Protected AccessToken doInBackground (String...params) {
                try{
                    return mTwitter.getOAuthAccessToken(mRequestToken, params[0]);
                } catch(TwitterExceptione){
                    e.printStackTrace();
                }
                return null;
            }

            @ Override
            protected void onPostExecute(AccessToken accessToken){
                TwitterUtils.storeAccessToken(mActivity, accessToken);
                PostTwitter postTwitter = new PostTwitter (mActivity);
                postTwitter.share(mActivity);
            }
        };
        task.execute(mOAuthVerifier);
    }
}

android

2022-09-30 20:30

2 Answers

AsyncTask no longer has parallel background processing from API 13, so

If the OS version of the terminal is different

If AsyncTask processing is called multiple times that are not synchronized,

You may or may not be called twice.


2022-09-30 20:30

I was able to solve this problem.
In fact, the processing was wrong in a different place from AsyncTask.
The wrong place was where the dialog was displayed, and onPageStarted and onPageFinished were called twice:When I looked into it, it seems that the terminal sometimes calls me several times.Therefore, I was able to make one-time tweets like if(!mIsPageFinished) in onPageFinished.

public void showTwitterDialog(){
        final Dialog twitterDialog = new Dialog(mActivity); {
            twitterDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            twitterDialog.setContentView(R.layout.twitter_dialog);
            WebView webView= (WebView) twitterDialog.findViewById (R.id.twitter_webview);
            webView.loadUrl(mOAuthURL);
            webView.setWebViewClient(newWebViewClient(){

                @ Override
                public void onPageStarted (WebView view, String url, Bitmap favicon) {
                    super.onPageStarted (view, url, favicon);
                }

                @ Override
                public boolean shouldOverrideUrlLoading (WebView view, String url) {
                    return false;
                }

                @ Override
                public void onPageFinished (WebView view, String url) {
                    super.onPageFinished(view, url);
                    if(!mIsPageFinished){
                        if(url.contains("oauth_verifier")}
                            Uri = Uri.parse(url);
                            mOAuthVerifier=uri.getQueryParameter("oauth_verifier");
                            twitterDialog.dismiss();
                            getAccessToken();
                            mIsPageFinished=true;
                        } else if(url.contains("denied")}
                            twitterDialog.dismiss();
                        }
                    }
                }
            });
            twitterDialog.show();
        }
    }


2022-09-30 20:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.