与旋转支持异步

时间:2012-04-16 20:12:28

标签: android android-asynctask orientation

编辑:所以我尝试使用旋转支持,但每次旋转设备时,对话框会弹出任何关于我做错了什么的想法,或者为什么没有让对话框显示在方向更改上?

 package com.ondrovic.bbym;

 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;

 import android.app.Activity;
 import android.app.Dialog;
 import android.app.ProgressDialog;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Environment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
 import android.widget.TextView;

 public abstract class Dashboard extends Activity {
public static final boolean usePrettyGoodSolution = false;
private ProgressDialog PROGRESS_BAR;
private Download task = null;
private static final int PROGRESS = 0;
public static File rDIR = Environment.getExternalStorageDirectory();
public static final String fDIR = "/Android/data/Bestbuy Mobile/database/";
public static final String fNAME = "bestbuy.db";
public static final String fURL = "http://db.tt/5JTRpA8q";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    task = (Download) getLastNonConfigurationInstance();

}

@Override
public Object onRetainNonConfigurationInstance() {
    task.detach();
    return (task);
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onRestart() {
    super.onRestart();
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onStart() {
    super.onStart();
}

@Override
public void onStop() {
    super.onStop();
}

public void onClickHome(View v) {
    goHome(this);
}

public void onClickUpdate(View v) {
    dbDIR(fDIR);
    //new Download().execute(fURL);
    if (task == null) {
        task = new Download(this);
        task.execute(fURL);
    } else {
        task.attach(this);
    }
}

public void onClickEmail(View v) {
    // title_bar email : price quotes :-)

    if (getTitle().toString().equals("Individual")) {
        try {
            Intent email = new Intent(Intent.ACTION_SEND);
            email.setType("message/rfc822");
            email.putExtra(Intent.EXTRA_SUBJECT,
                    "BBYM Individual Plan Comparison");
            email.putExtra(Intent.EXTRA_TEXT, "\nAT&T Individual Plan"
                    + "\nMinutes Package: " + Individual_ATT.packageTalk
                    + "\nMessage Package: " + Individual_ATT.packageText
                    + "\nData Package: " + Individual_ATT.packageData
                    + "\nPrice: " + Individual_ATT.packagePrice
                    + "\n\nSprint Individual Plan" + "\nPackage: "
                    + Individual_SPRINT.packageInfo + "\nPrice: "
                    + Individual_SPRINT.packagePrice
                    + "\n\nVerizon Individual Plan" + "\nPackage: "
                    + Individual_VERIZON.packageTalk + "\nData: "
                    + Individual_VERIZON.packageData + "\nPrice: "
                    + Individual_VERIZON.packagePrice);

            try {
                startActivity(Intent
                        .createChooser(email, "Send Comparison"));
            } catch (android.content.ActivityNotFoundException ex) {

            }
        } catch (Exception e) {

        }
    }

    if (getTitle().toString().equals("Family")) {
        try {
            Intent email = new Intent(Intent.ACTION_SEND);
            email.setType("message/rfc822");
            email.putExtra(Intent.EXTRA_SUBJECT,
                    "BBYM Family Plan Comparison");
            email.putExtra(Intent.EXTRA_TEXT, "\nAT&T Family Plan"
                    + "\nMinutes Pacakge: "
                    + Family_ATT.packageTalk
                    + "\nMessage Package: "
                    + Family_ATT.packageText
                    + "\nAdditional Lines: "
                    + Family_ATT.packageXtraLn
                    + "\nData Package Line 1: "
                    + Family_ATT.packageData1
                    + "\nData Package Line 2: "
                    + Family_ATT.packageData2
                    + "\nData Package Line 3: "
                    + Family_ATT.packageData3
                    + "\nData Pacakge Line 4: "
                    + Family_ATT.packageData4
                    + "\nData Package Line 5: "
                    + Family_ATT.packageData5
                    + "\nPrice: "
                    + Family_ATT.packagePrice
                    + "\n\nSprint Family Plan"
                    + "\nPacakge: "
                    + Family_SPRINT.packageInfo
                    + "\nAdditional Lines: "
                    + Family_SPRINT.packageXtraLn
                    + "\nLine 1 Type: "
                    + Family_SPRINT.packageLine1
                    + "\nLine 1 Data: "
                    + Family_SPRINT.dataPriceL1
                    + "\nLine 2 Type: "
                    + Family_SPRINT.packageLine2
                    + "\nLine 2 Data: "
                    + Family_SPRINT.dataPriceL2
                    + "\nLine 3 Type: "
                    + Family_SPRINT.packageLine3
                    + "\nLine 3 Data: "
                    + Family_SPRINT.dataPriceL3
                    + "\nLine 4 Type: "
                    + Family_SPRINT.packageLine4
                    + "\nLine 4 Data: "
                    + Family_SPRINT.dataPriceL4
                    + "\nLine 5 Type: "
                    + Family_SPRINT.packageLine5
                    + "\nLine 5 Data: "
                    + Family_SPRINT.dataPriceL5
                    + "\nPrice: "
                    + Family_SPRINT.packagePrice
                    + "\n\nVerzon Family Plan"
                    + "\nPackage: "
                    + Family_VERIZON.packageTalk
                    + "\nAdditional Lines: "
                    + Family_VERIZON.packageXtraLn
                    + "\nData Pacakge Line 1: "
                    + Family_VERIZON.packageData1
                    + "\nData Pacakge Line 2: "
                    + Family_VERIZON.packageData2
                    + "\nData Pacakge Line 3: "
                    + Family_VERIZON.packageData3
                    + "\nData Pacakge Line 4: "
                    + Family_VERIZON.packageData4
                    + "\nData Pacakge Line 5: "
                    + Family_VERIZON.packageData5
                    + "\nPrice: "
                    + Family_VERIZON.packagePrice);

            try {
                startActivity(Intent
                        .createChooser(email, "Send Comparison"));
            } catch (android.content.ActivityNotFoundException ex) {

            }
        } catch (Exception e) {

        }
    }

    if (getTitle().toString().equals("Broadband")) {
        try {
            Intent email = new Intent(Intent.ACTION_SEND);
            email.setType("message/rfc822");
            email.putExtra(Intent.EXTRA_SUBJECT,
                    "BBYM Broadband Plan Comparison");
            email.putExtra(Intent.EXTRA_TEXT, "\nAT&T Broadband Plan"
                    + "\nPackage: " + Broadband_ATT.pacakgeData
                    + "\nPrice: " + Broadband_ATT.packagePrice
                    + "\n\nSprint Broadband Plan" + "\nPackage: "
                    + Broadband_SPRINT.pacakgeData + "\nPrice: "
                    + Broadband_SPRINT.packagePrice
                    + "\n\nVerizon Broadband Plans" + "\nPacakge: "
                    + Broadband_VERIZON.pacakgeData + "\nPrice: "
                    + Broadband_VERIZON.packagePrice);

            try {
                startActivity(Intent
                        .createChooser(email, "Send Comparison"));
            } catch (android.content.ActivityNotFoundException ex) {

            }
        } catch (Exception e) {

        }
    }

    if (getTitle().toString().equals("Embedded")) {
        try {
            Intent email = new Intent(Intent.ACTION_SEND);
            email.setType("message/rfc822");
            email.putExtra(Intent.EXTRA_SUBJECT,
                    "BBYM Embedded Plan Comparison");
            // email.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new
            // StringBuilder()
            // .append("<p><b>BBYM Embedded Plan Comparison</b></p>")
            // .append("<p>This is a test</>") .toString()));

            try {
                startActivity(Intent
                        .createChooser(email, "Send Comparison"));
            } catch (android.content.ActivityNotFoundException ex) {

            }
        } catch (Exception e) {

        }
    }
}

public void goHome(Context context) {
    final Intent intent = new Intent(context, Home.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    context.startActivity(intent);
}

@Override
public void setContentView(int layoutID) {
    if (!usePrettyGoodSolution) {
        super.setContentView(layoutID);
        return;
    }

    @SuppressWarnings("unused")
    Configuration c = getResources().getConfiguration();
    int size = c.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
    boolean isLarge = (size == Configuration.SCREENLAYOUT_SIZE_LARGE);
    boolean isXLarge = (size == Configuration.SCREENLAYOUT_SIZE_XLARGE);
    boolean addFrame = isLarge || isXLarge;

    // if (isLarge) System.out.println ("Large screen");
    // if (isXLarge) System.out.println ("XLarge screen");

    int finalLayoutId = addFrame ? R.layout.large : layoutID;
    super.setContentView(finalLayoutId);

    if (addFrame) {
        LinearLayout frameView = (LinearLayout) findViewById(R.id.frame);
        if (frameView != null) {

            // If the frameView is there, inflate the layout given as an
            // argument.
            // Attach it as a child to the frameView.
            LayoutInflater li = ((Activity) this).getLayoutInflater();
            View childView = li.inflate(layoutID, null);
            if (childView != null) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT, 1.0F);
                frameView.addView(childView, lp);
                // childView.setBackgroundResource (R.color.background1);
            }

        }
    }
}

public void setTitleFromActivityLabel(int textViewID) {

    TextView tv = (TextView) findViewById(textViewID);
    if (tv != null) {
        tv.setText(getTitle());
    }

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case PROGRESS:
        PROGRESS_BAR = new ProgressDialog(this);
        PROGRESS_BAR.setTitle("Updater");
        PROGRESS_BAR.setMessage("Updating database");
        PROGRESS_BAR.setIndeterminate(false);
        PROGRESS_BAR.setMax(100);
        PROGRESS_BAR.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        PROGRESS_BAR.setCancelable(false);
        PROGRESS_BAR.show();
        return PROGRESS_BAR;
    default:
        return null;
    }
}

class Download extends AsyncTask<String, String, String> {
    Dashboard activity = null;

    Download(Dashboard activity) {
        attach(activity);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(PROGRESS);
    }

    @Override
    protected String doInBackground(String... aurl) {
        try {
            URL url = new URL(fURL);
            HttpURLConnection con = (HttpURLConnection) url
                    .openConnection();
            con.setRequestMethod("GET");
            con.setDoOutput(true);
            con.connect();

            int fSIZE = con.getContentLength();

            FileOutputStream out = new FileOutputStream(new File(rDIR
                    + fDIR, fNAME));

            InputStream in = con.getInputStream();

            byte[] buffer = new byte[1024];
            int len = 0;
            long total = 0;

            while ((len = in.read(buffer)) > 0) {
                total += len;
                publishProgress("" + (int) ((total * 100) / fSIZE));
                out.write(buffer, 0, len);
            }
            out.close();
        } catch (Exception e) {

        }
        return null;
    }

    @Override
    protected void onProgressUpdate(String... progress) {
        if (activity == null) {

        } else {
            PROGRESS_BAR.setProgress(Integer.parseInt(progress[0]));
        }

        //PROGRESS_BAR.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String fURL) {
        dismissDialog(PROGRESS);
    }

    void detach() {
        activity = null;
    }

    void attach(Dashboard activity) {
        this.activity = activity;
    }
}

public void dbDIR(String dirName) {
    File nDIR = new File(rDIR + dirName);
    if (!nDIR.exists()) {
        nDIR.mkdirs();
    }
}
 }

0 个答案:

没有答案